combining 2 random numbers.

Started by
1 comment, last by Numsgil 14 years, 6 months ago
Hey Me and a m8 are having a argument over the result of combining 2 random numbers. Say you have a rand() that returns a 16bit uint. Now you will have a and b that are returned from rand() after each other. Can you make a 32 bit random number by bit shifting the first number 16bits, and replace the first 16bits with number b. Or rather what would the quality would the "randomness" 32bit number be?
- Me
Advertisement
It is a perfectly reasonable way to make larger random values from smaller ones. The quality and distribution of it, however, depends entirely on the quality and distribution of the original random values. If the two 16-bit values are uniformly distributed and independent, then the 32-bit value is uniformly distributed.
It's a reasonable method if you want something quick and dirty, and it's for a game anyway so it doesn't matter, but it would be a pretty terrible method if you were doing scientific computing or cryptography (ie: key codes for some game).

To understand why, read Chapter 7 in Numerical Recipes in C. A quick summary, though, is that most possible pseudo random number generators exhibit rather non random behaviors in higher dimensions (ie: taking lots of sets of (say) 15 consecutive random numbers and forming a point cloud in 15 dimensional space will often result in a pattern). So to get a number generator which does it "right" for some given bit width requires quite a bit of confusing higher level math and a bit of black magic.
[size=2]Darwinbots - [size=2]Artificial life simulation

This topic is closed to new replies.

Advertisement