[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
Solved

# How to generate a random 32bit number?

Posted on 1998-12-08
Medium Priority
352 Views
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
Question by:jonem
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points

LVL 8

Accepted Solution

ID: 1254957
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

LVL 84

Expert Comment

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

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use switch statements in the C programming language.
###### Suggested Courses
Course of the Month13 days, 6 hours left to enroll