Random number with no repating. - 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)
+--- Thread: Random number with no repating. (
/showthread.php?tid=416675)
Random number with no repating. -
dusk - 17.02.2013
How do i get a random number WITHOUT repeating. I mean if the number is used,it can't be used again. Any help?
Re: Random number with no repating. -
CJ101 - 17.02.2013
// somewhere on top of script
Код:
new TheNumber = random(15656);
if(TheNumber == LastNumber)
{
TheNumber = random(15656);
}
else
{
LastNumber = TheNumber;
//other code here
}
Re: Random number with no repating. -
Misiur - 17.02.2013
pawn Код:
#define MAX_MEMORY 20
#define A_LOT 5
new memory[MAX_MEMORY];
stock TrulyRandom() {
new tmp;
static trials;
while(!tmp) {
tmp = random(A_LOT);
for(new i = 0; i != trials; ++i) {
if(memory[i] == tmp) {
tmp = 0;
break;
}
}
}
++trials;
return tmp;
}
Re: Random number with no repating. -
[MG]Dimi - 17.02.2013
Quote:
Originally Posted by cj101
// somewhere on top of script
Код:
new TheNumber = random(15656);
if(TheNumber == LastNumber)
{
TheNumber = random(15656);
}
else
{
LastNumber = TheNumber;
//other code here
}
|
And if it get's same number again?
pawn Код:
stock GetRandomNumber(maximum,lastnumber)
{
new i=-1;
while(i == -1)
{
i=random(maximum+1);
if(i == lastnumber) i = -1;
}
return i;
}
I created this stock for you now. Params are obvious. If you want to get new random number between 0 and 100 and it should differ from 56 (example) you would do
pawn Код:
new number = GetRandomNumber(100,56);