SA-MP Forums Archive
4digit random numbers - Printable Version

+- SA-MP Forums Archive (https://sampforum.blast.hk)
+-- Forum: SA-MP Scripting and Plugins (https://sampforum.blast.hk/forumdisplay.php?fid=8)
+--- Forum: Scripting Help (https://sampforum.blast.hk/forumdisplay.php?fid=12)
+---- Forum: Help Archive (https://sampforum.blast.hk/forumdisplay.php?fid=89)
+---- Thread: 4digit random numbers (/showthread.php?tid=212984)



4digit random numbers - THE_KNOWN - 18.01.2011

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


Re: 4digit random numbers - Universal - 18.01.2011

use:

Above the main():

pawn Код:
new pBankPin [ MAX_PLAYERS ];
And heres the functions:
pawn Код:
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.


Re: 4digit random numbers - Macluawn - 18.01.2011

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

My version of your 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.


Re: 4digit random numbers - THE_KNOWN - 18.01.2011

afaik random only generates numbers ranging between 0 to max(9999) so i think this wont work (havent tested).


Re: 4digit random numbers - THE_KNOWN - 18.01.2011

@macluan how do make a 8digit number which wont repeat?(for phonenumbers)


Re: 4digit random numbers - Macluawn - 18.01.2011

the same as my other example, but changing the minimum and the maximum ammount.
Код:
phonenumber = 10000000+random(89999999);



Re: 4digit random numbers - THE_KNOWN - 18.01.2011

but how can i make it exclude already selected nos?


Re: 4digit random numbers - THE_KNOWN - 18.01.2011

nvm just understood. btw thanks for the help man