• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 359
  • 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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

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