Random Number - 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 (
/showthread.php?tid=312539)
Random Number -
FreeSoul - 21.01.2012
How can I select a random number from let's say,1 to 100 ?
Thanks for the help
Re: Random Number -
Konstantinos - 21.01.2012
pawn Код:
new
number = random( 100 );
Re: Random Number -
[XST]O_x - 21.01.2012
If you want to randomize between a minimal and a maximal number you can use randomex:
pawn Код:
stock randomex(min, max)
{
return random(max - min) + min;
}
For example:
random(5) can return 0,1,2,3,4,5 while randomex(3,5) can return 3,4,5.
Re: Random Number -
Konstantinos - 21.01.2012
Nice Example [XST]O_x. Of course it's better way that the simplest because we can specify the numbers from..to..
Re: Random Number -
[XST]O_x - 21.01.2012
Quote:
Originally Posted by Dwane
Nice Example [XST]O_x. Of course it's better way that the simplest because we can specify the numbers from..to..
|
Thanks
The function is not mine btw.
Re: Random Number -
Chris White - 21.01.2012
Nice one, dude. Keep it up.
Re: Random Number -
[HiC]TheKiller - 21.01.2012
Quote:
Originally Posted by [XST]O_x
If you want to randomize between a minimal and a maximal number you can use randomex:
pawn Код:
stock randomex(min, max) { return random(max - min) + min; }
For example:
random(5) can return 0,1,2,3,4,5 while randomex(3,5) can return 3,4,5.
|
Why would you use a stock for that? It's quite a lot slower than using a define.
pawn Код:
#define randomex(%1,%2) random(%1 - %2) + %2
Also, 1 - 100 can simply be done by
pawn Код:
new randomnum = random(100) + 1;
//Numbers start at 0 and would go to 99 but we added the +1 so it went from 1 -> 100.
Re: Random Number -
Gh05t_ - 21.01.2012
Quote:
Originally Posted by [HiC]TheKiller
Also, 1 - 100 can simply be done by
pawn Код:
new randomnum = random(100) + 1; //Numbers start at 0 and would go to 99 but we added the +1 so it went from 1 -> 100.
|
This is also the equivalent to
Re: Random Number -
Macluawn - 21.01.2012
No it isnt.
Your example would go from 0 to 98.
Re: Random Number -
[HiC]TheKiller - 21.01.2012
Quote:
Originally Posted by Macluawn
No it isnt.
Your example would go from 0 to 98.
|
It's actually 0 - 99 because it's the same as just doing random(100).