Amazon Interview Question
Software Engineer / DevelopersCountry: United States
An example of a simple pseudo-random number generator is the Multiply-with-carry method invented by George Marsaglia. It is computationally fast and has good (albeit not cryptographically strong) randomness properties (note that this example is not thread safe):[6]
m_w = <choose-initializer>; /* must not be zero */
m_z = <choose-initializer>; /* must not be zero */
uint get_random()
{
m_z = 36969 * (m_z & 65535) + (m_z >> 16);
m_w = 18000 * (m_w & 65535) + (m_w >> 16);
return (m_z << 16) + m_w; /* 32-bit result */
}
You should initially seed the psudo random generator with a number such as the current time since that will be different at every runtime.
- luckless March 23, 2012Otherwise just hook up a geiger counter to the computer and look at the radioactive decay, that is truly random.