Random Number
#1

How can I select a random number from let's say,1 to 100 ?
Thanks for the help
Reply
#2

pawn Код:
new
    number = random( 100 );
Reply
#3

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

Nice Example [XST]O_x. Of course it's better way that the simplest because we can specify the numbers from..to..
Reply
#5

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

Nice one, dude. Keep it up.
Reply
#7

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

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

pawn Код:
new rand = random(99+1);
Reply
#9

No it isnt.
Your example would go from 0 to 98.
Reply
#10

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).
Reply


Forum Jump:


Users browsing this thread: 3 Guest(s)