SA-MP Forums

Go Back   SA-MP Forums > SA-MP Scripting and Plugins > Scripting Help > Help Archive

 
 
Thread Tools Display Modes
Old 18/01/2011, 11:43 AM   #1
THE_KNOWN
Gangsta
 
Join Date: Dec 2010
Posts: 702
Reputation: 60
Default 4digit random numbers

as the title says code a random number system? i want it to give a random bank pin and it should only be 4digit and non repeating
THE_KNOWN is offline  
Old 18/01/2011, 11:54 AM   #2
Universal
Huge Clucker
 
Universal's Avatar
 
Join Date: Jan 2010
Posts: 449
Reputation: 27
Default Re: 4digit random numbers

use:

Above the main():

pawn Code:
new pBankPin [ MAX_PLAYERS ];

And heres the functions:
pawn Code:
stock GenerateBankPin() {
 new pin =  random(9999);
 return pin;
}

stock GivePlayerBankPin ( playerid ) {
new bpin = GenerateBankPin();

for ( new i = 0; i < MAX_PLAYERS ; i++ ) {
 if ( IsPlayerConnected ( i ) && pBankPin [ i ] ==  bpin ) {
  bpin = GenerateBankPin();
 }
}
 return bpin;
}

Havent tested it.
Universal is offline  
Old 18/01/2011, 11:57 AM   #3
Macluawn
Gangsta
 
Macluawn's Avatar
 
Join Date: Aug 2010
Location: Latvia
Posts: 613
Reputation: 78
Default Re: 4digit random numbers

with your example the pin could be 3digit, 2digit and even 1digit number.
use
Code:
pin =  1000+random(8999);
with this the pin minimum can be 1000 and maximum of 9999 (1000+8999=9999)

My version of your code -
Code:
stock GenerateBankPin(playerid) 
{
  new pin =  1000+random(8999); //generates a number
  for ( new i = 0; i < GetMaxPlayers() ; i++ )  //loops through all the server player slots (defined in server.cfg)
  {
    if ( IsPlayerConnected ( i ) && pBankPin [ i ] ==  bpin && i!=playerid) pin =  1000+random(8999); //checks if player in that slot is connected and if its pin is the same as the playerid's then it generates a new pin.
  }
  return pin;
}
This isn't the best sollution offcourse and in some cases the number may be the same.
Macluawn is offline  
Old 18/01/2011, 11:58 AM   #4
THE_KNOWN
Gangsta
 
Join Date: Dec 2010
Posts: 702
Reputation: 60
Default Re: 4digit random numbers

afaik random only generates numbers ranging between 0 to max(9999) so i think this wont work (havent tested).
THE_KNOWN is offline  
Old 18/01/2011, 11:59 AM   #5
THE_KNOWN
Gangsta
 
Join Date: Dec 2010
Posts: 702
Reputation: 60
Default Re: 4digit random numbers

@macluan how do make a 8digit number which wont repeat?(for phonenumbers)
THE_KNOWN is offline  
Old 18/01/2011, 12:01 PM   #6
Macluawn
Gangsta
 
Macluawn's Avatar
 
Join Date: Aug 2010
Location: Latvia
Posts: 613
Reputation: 78
Default Re: 4digit random numbers

the same as my other example, but changing the minimum and the maximum ammount.
Code:
phonenumber = 10000000+random(89999999);
Macluawn is offline  
Old 18/01/2011, 12:13 PM   #7
THE_KNOWN
Gangsta
 
Join Date: Dec 2010
Posts: 702
Reputation: 60
Default Re: 4digit random numbers

but how can i make it exclude already selected nos?
THE_KNOWN is offline  
Old 18/01/2011, 12:14 PM   #8
THE_KNOWN
Gangsta
 
Join Date: Dec 2010
Posts: 702
Reputation: 60
Default Re: 4digit random numbers

nvm just understood. btw thanks for the help man
THE_KNOWN is offline  
 

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[HELP] Random Numbers psoftware Help Archive 7 31/12/2010 06:14 PM
[Help] Random numbers Rainbow_Six Help Archive 4 21/10/2010 06:08 PM
Random Cellphone Numbers Torran Help Archive 1 02/04/2010 07:51 PM
[HELP] Random numbers? SiJ Help Archive 6 09/08/2009 02:14 PM
Need help with random numbers... *BueNoOo* Help Archive 11 18/06/2009 04:13 PM


All times are GMT. The time now is 09:08 AM.


Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.