1. ## srand() question

Hi,

I have a program that takes a seed and generates a random number using srand and rand, however there is another requirement to it that I don't understand and am hoping that you may make sense of. The instructions say:

The random number seed must be set using the srand function in stdlib.h. Random number must be generated by bitwise ANDing (&) 0x7FFF with the value return by the rand function in stdlib.h.
Do you know what bitwise ANDing 0x7FFF is? The code I have is:

Code:
```#include <iostream>
#include <stdlib.h>

using namespace std;

int main(int argc, char *argv[])
{
int seed;
int randnum;

seed = 65445321;

cout << "Generating a random number...\n";

srand(seed);

randnum = rand();

cout << randnum << "\n";

system("Pause");
return 0;
}```

2. It means that to get each random value, do this:

Code:
`random_value = rand() & 0x7FFF;`

3. Also, people usually seed srand() with the current time, otherwise you'd always get the same numbers every time you run the program:
Code:
`srand( time() );`

4. I have a quick question related to this. You guys, and everywhere else, always use time() as the seed for the random number generator. However, in my game programming book it always uses GetTickCount() as the seed. Will one of them generate numbers more randomly than the others?

5. Originally Posted by mikeman118
I have a quick question related to this. You guys, and everywhere else, always use time() as the seed for the random number generator. However, in my game programming book it always uses GetTickCount() as the seed. Will one of them generate numbers more randomly than the others?
The seed doesn't influence how random the numbers are, only the particular sequence you end up getting. You just need to pass SOME value that isn't the same every time. Both time() and GetTickCount() work for that.

6. Also, GetTickCount() is a Windows specific function, so for portable code it's better to use time() since it's in all C/C++ platforms.

7. Originally Posted by cpjust
Also, people usually seed srand() with the current time, otherwise you'd always get the same numbers every time you run the program:
Code:
`srand( time() );`
time() takes a parameter, so most people use
Code:
`srand(time(0));`
To be completely proper, one should use
Code:
`srand((unsigned)time(0));`
or, with C++-style casts,
Code:
`srand(static_cast<unsigned>(time(0)));`
Some people like to use NULL instead of 0 as well.

 There's lots of information around. Google for "srand time". Here's one hit.

8. Well, to be completely proper you need to generate a hash of the value returned by 'time', to take into account systems where the result is 64 bits, according to Prelude's website.

9. Oops, I forgot about the parameter. I don't need to use time() that much.

10. Well, to be completely proper you need to generate a hash of the value returned by 'time', to take into account systems where the result is 64 bits, according to Prelude's website.
Well, yes, it's true. http://eternallyconfuzzled.com/arts/jsw_art_rand.aspx

11. Do you know what bitwise ANDing 0x7FFF is?
That's called a bit mask. It "masks-off" bit-15 and above*, forcing those bits to zero. Bits 0-14 are unaffected.

After masking, your random number cannot exceed be 7FFF hexadecimal (32767 decimal). Any numbers smaller than that will be unaffected. Higher-value numbers will be altered.

*Note that bits are counted from zero, so "bit-15" is actually the 16th bit.