1

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++

svick
  • 236,525
  • 50
  • 385
  • 514
Mark
  • 626
  • 5
  • 20

2 Answers2

2
  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

theblindprophet
  • 7,767
  • 5
  • 37
  • 55
  • When you post an answer at the same exact time, but the other person's is better. Nice post! – Andrew Li Apr 24 '16 at 04:27
  • Gladly! Thanks for your post, it helped me too :) – Andrew Li Apr 24 '16 at 04:32
  • 1
    According to you are pseudo and true random numbers the same?.. Moreover the definition you have given for true random numbers is the one for pseudo random numbers as stated by Wikipedia. – Mark Apr 24 '16 at 04:50
  • @HiteshSikka I never mentioned that they were the same, they are generate through different processes – Andrew Li Apr 24 '16 at 19:50
  • I would add that after having generated a seed, 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 Apr 25 '16 at 07:35
0

A short summary:

A true random number is a number you cannot calculate under any circumstances that is generated by some event, while a random number calculated by the Rand() function is calculated.

There are multiple ways to calculate such a number, some being more complex than others.

See this question for more details on how it works in (objective- in this case) C.

Community
  • 1
  • 1
CodenameLambda
  • 1,486
  • 11
  • 23