• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 355
  • Last Modified:

How to generate a random 32bit number?

It appears the Visual C++ implements the rand() function such that it returns a value from 0-2^15.  How can I generate a random unsigned int?  The full 32bits...
0
jonem
Asked:
jonem
1 Solution
 
Answers2000Commented:
VC rand() generates a random number between 0 and x7fff (32767).  The later being the constant given by RAND_MAX

I guess a crude algorithm is to use multiple random numbers and shift

int r1 = rand() & 0x7FFF  ;
inr r2 = rand() & 0x7FFF ;
int r3 = rand() & 0x0003 ;

int rnd = ( (r1) | (r2 << 15) | ( r3 << 30 ) ) ;

0
 
ozoCommented:
here's a slightly less crude algorithm:
http://www.math.keio.ac.jp/~nisimura/random/int/mt19937int.c
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now