rand()%???

Started by
15 comments, last by The Alchemist 23 years, 11 months ago
just a curiosity: someone here know why the rand() funtion call is so weird? -i put gel in my hair. now i´m looking like a backstreet boy!!!
"Everything works out in the end, if it doesn't then it is not the end"
Advertisement
What is so weird about it?

rand() produces a random number and the mod operator (%) keeps it in a certain range. So for a random number from 0 to 99 you would have rand()%100.



Andrew
the weird is that in other funcion you put the variables inside the "()" and in the rand you put it outside ( rand()%x ) ,why the funcion don´t was "rand(x)"

- yo know, i like japan...
"Everything works out in the end, if it doesn't then it is not the end"
because all the rand function does is return a value. You don''t need to send it any numbers to begin with. Rand just grabs a number using the current clock time. So lets just say we call rand() and we get back a value. Now this value could be any value with in the integer range. So for our purposes 100. Now if I wanted a number between 0 and 50 a random value of 100 isn''t going to do. So we use the modulous operator. 100 % 50= the remainder of 100/50. Which in this case turns out to be zero.
Joseph FernaldSoftware EngineerRed Storm Entertainment.------------------------The opinions expressed are that of the person postingand not that of Red Storm Entertainment.
i get it!!! so really the funcion rand() only get a number in the clock that is filter by the %, i bet that my C teacher don´t know that

-... they made that increditable P.E. uniforms...

"Everything works out in the end, if it doesn't then it is not the end"
rand() does not use the clock to get its values. rand() is often a very simple function somewhat like this:

unsigned long rand(void){  return (seed * A) + B;}


A and B are replaced with constant unsigned integers; they are chosen in the hopes of producing a random-looking sequence. seed is a global variable initialized by the function srand(). Many programmers use the clock in their call to srand(), but rand() itself will not use the clock.

Why? An important part of (pseudo-)random numbers is that they can be made to repeat. If you run a program and initialize the seed with the call srand(42);, and you later run the program using the same seed, you will get exactly the same sequence of numbers. This is important, for example, when trying to debug a program that fails sometimes depending on what numbers come up. This is one reason why rand() will never use the clock. (Another is that the clock is not very random.)

Note that the above choice for rand() is a very primitive pseudo-random number function. There are others which will appear more random (yet will produce repeatable result when initializing with the same seed). One of the faster, better PRNG is called Mersenne Twister. Easy to find with your favorite search engine.

---- --- -- -
Blue programmer needs food badly. Blue programmer is about to die!
If you really don''t like the rand() function, make a wrapper function like:

int GetRandomNumber(int range)
{
return rand() % range;
}

problem solved!

- Daniel
my homepage
- DanielMy homepage
As an additional piece of info, the modulus operator ''%'' takes quite a bit of processing (which may be undesirable in a game engine). If possible, use the ''left shift operator'' and/or ''right shift operator'' to get the rand() number within the range you desire.

This is easiest if you want numbers in the range from 0 to 2^x - 1 (2 to the power x minus 1) where x is anything from 0 to 32.

e.g.
0 to 15 (2^4 - 1)
0 to 31 (2^5 - 1)
0 to 63 (2^6 - 1)

To get within a range like this, move the bits to the right by 32 - x.

e.g. For a random number between 0 and 15:

mynumber = rand() >> 28;
//28 is 32-4 and 2^4-1 is 15...get it!!!

This method (if usable) is far faster than using the mod ''%'' operator.

Marty
I hope your C teacher did know how the rand() function call worked...hell I''m a newbie and even I figured it out for myself

"Born of a Broken Man, but not a broken man."
------------------------------"If a job's worth doing it's worth getting someone else to do it for you....."
A little research into functions and the % operator would have solved this prob...

This topic is closed to new replies.

Advertisement