Search code examples
randomprogramming-languages

Truly random numbers


I have two questions here:

  1. Please explain me the meaning of truly random number. I Read many articles but I was not able to understand the meaning.

  2. Please explain me the code behind Rand() function in any language preferably c++


Solution

    1. A truly random number is such that was determined by a physically random event (unpredictable events in nature). e.g. (radioactive decay of an atom, atmospheric noise, etc). The reason this is such is because the computer does not have the capability of being truly random on its own, however, nature is. So if the computer measures some random value in nature then the number is "truly random," but pseudo is where the computer generates a seed (C++ uses time to generate seed) and the random number appears to be random, but could be predicted.

    "The computer uses a function to calculate random numbers. That means: having the same seed, we get the same row of random numbers every time. Therefore pseudo random numbers." – Ralph M. Rickenbach

    1. C++ uses time, number of seconds since midnight Jan 1, 1970. So when you tell the computer to produce a random number it takes the number of seconds since Jan 1, 1970 (a very large number, the seed), sends it through an algorithm, and then chops it up accordingly to satisfy the range you specified.

    This causes the computer to read its clock to obtain the value for the seed automatically. Function time returns the number of seconds that have passed since midnight on January 1, 1970. This value is converted to an unsigned integer and used as the seed to the random number generator.

    This is of course a simplification, more can be read online. Below are references.

    References:

    Truly random

    c++ docs