How do I get a larger number by stringing calls to rand together?
For instance, to get a 32 bit random number from 2 16 bit random numbers
(rand() << 16)|rand()
This assumes that int is at least 32 bits, otherwise you'll have to add some
casts.
If RAND_MAX is 0x7fff as in John Harrisons example, the formula above will
not work as wanted.
rand()*(RAND_MAX+1)+rand() is better in that case.
How about using floating point:
double n1=double(rand())/(RAND_MAX+1) will result in a number between 0 and
1 (1 not included).
The "range" can then be increased by:
double n2=(rand()+n1)/(RAND_MAX+1);
This can be repeated until the number of bits are achieved:
double n3=(rand()+n3)/(RAND_MAX+1);
This would give 45 bits if RAND_MAX is 15 bits.
Then finally you can map this into the range you want:
int r=int(n3*1e9)+1;
Niels Dybdahl