Random number generator

Michael Condict condict at cs.vu.nl
Sat Dec 23 02:25:21 AEST 1989


In article <83943 at linus.UUCP> rtidd at mwsun.mitre.org writes:
|Greetings 
|
|I was trying to generate a random number bewteen 0 and maxlongint (i.e. 2^32)
|on my machine (80386 with Interactive). The rand() function that I have
|returns a psuedo-random number between 0 and MAXINT (which in this case
|is 32768). I thought about generating two random numbers and multiplying
|them together, but I realized that would produce a bell-curve-shaped
|distrubution (the numbers in the middle would be more likely to occur).
|
|Does anyone have any ideas or any code that generates random numbers,
|or anyplace that I can check to find such code?
|

Generate the two 16-bit numbers and concatenate them:

	long_random = (short_random_1 << 16) + short_random_2;

Or, if you prefer:

	long_random = short_random_1 * 65536 + short_random_2;

If the rand function generates uniformly distributed random shorts,
this will obviously produce uniformly distributed random longs.
-- 
Michael Condict		condict at cs.vu.nl
Vrije University
Amsterdam



More information about the Comp.lang.c mailing list