Solved

How to generate a random 32bit number?

Posted on 1998-12-08
2
297 Views
Last Modified: 2010-04-15
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
Comment
Question by:jonem
2 Comments
 
LVL 8

Accepted Solution

by:
Answers2000 earned 100 total points
Comment Utility
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

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

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now