Could someone tell me how I should use the rand() function when i want a random number within an area.
Ex:
If I want the rand function to return a random number between
1-5.
Could someone tell me how I should use the rand() function when i want a random number within an area.
Ex:
If I want the rand function to return a random number between
1-5.
% gcc -v
Configured with: FreeBSD/i386 system compiler
Thread model: posix
gcc version 3.3.3 [FreeBSD] 20031106
Look in the board FAQ...
http://www.cprogramming.com/boardfaq.html#random
... here.
Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.
I know in assembler there is an instruction to get a random number. I'm not sure what it is, but if you can't find anything else, you might want to look into it.
int main()
{
int bob;
bob = rand()%6;
cout<<bob<<endl;
}
would output something between 0 and 5
- 0927
- a.k.a 0 9 two 7
If you want a lower bound like 100 and an upper bound lets say 300 you could do this
and if you had set high and low to the appropriate values you'd have a random number every time between 100 and 300PHP Code:
rand()%(high-low+1)-low;
I have a real nice class for this purpose which allows larger random numbers to be created (email [email protected] for the class, i dont want to post it into the public domain just yet)