4digit random numbers
#1

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
Reply
#2

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.
Reply
#3

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.
Reply
#4

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

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

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

but how can i make it exclude already selected nos?
Reply
#8

nvm just understood. btw thanks for the help man
Reply


Forum Jump:


Users browsing this thread: 7 Guest(s)