2

What is the difference between a truly random number and a pseudo random number in C++? (I guess, that it wants me to make my question longer, although I've put my problem as basically as I can. I thought that was the reason my last question wasn't received so well, I could be wrong. -_-. I did google this, and I've not been successful.)

Joshua Reid
  • 61
  • 1
  • 9
  • https://en.m.wikipedia.org/wiki/Random_number_generation#.22True.22_vs._pseudo-random_numbers – Richard Critten Jan 17 '17 at 01:28
  • Hello Joshua! To get the most out of the site it is important to ask good questions. A guide to asking questions is at: http://stackoverflow.com/help/how-to-ask If someone can answer your question in 30 seconds with google, that is not a good question. – Stephen Rauch Jan 17 '17 at 05:01

4 Answers4

4

Pseudo random numbers aren't truly random numbers because they are generated using a deterministic process. For example, if I wanted a random number between 0 and 4 and I had access to the current time in seconds, I could generate pseudo random numbers by applying modulus 5 to the time value and this would return a different number between 0 and 4.The reason the number isn't truly random is because it is possible to predict that number before generation by knowing the time. A truly random number could never be predicted.

You could Google "determinism" for more.

C4theWin
  • 49
  • 3
  • okay. This explanation was way easier to understand than that other explanation. That's what my teacher had us do. I didn't quite know what it did, but I knew the code to do it. Then he asked it on homework, and now I'm here. – Joshua Reid Jan 17 '17 at 01:50
  • At first i was really confused why I was grabbing ctime, but now it ALL makes sense. – Joshua Reid Jan 17 '17 at 01:51
  • Note that my example is likely different from what you're doing. This is just a guess, but if you're using srand and rand functions (cstdlib) note that they have nothing to do with time. You use the ctime library to pass a random number to srand, to be used as a seed for all subsequent rand calls. – C4theWin Jan 17 '17 at 02:50
  • Yea. It's that... cstdlib ctime srand – Joshua Reid Jan 17 '17 at 16:06
  • (rand() * whatever) + 1 – Joshua Reid Jan 17 '17 at 16:07
  • sorry (rand() % whatever) – Joshua Reid Jan 17 '17 at 19:46
4

This question could lead into a very deep philosophical rabbit hole. It is an open question if there really is a process in nature that can be truly non-deterministic. Quantum mechanics suggests that the universe is non-deterministic, but with deterministic probabilistic laws on some level. Nevertheless, this does not rule out the possibility that at some more fundamental level, a deterministic process resides, which simply appears random, at a larger scale. Randomness and chaos are intimately related, but they do not necessarily mean the same thing.

As a result, the word random could mean several different things. For practical purposes, we can consider randomness as a certain type of mathematical pattern, with certain characteristics. A random process typically follows some probabilistic distribution (i.e. white noise, pink noise, Gaussian, Poisson, ...), but it is not possible to practically predict individual results of when you sample the output of the random process.

A pseudo random number generator or PRNG uses an algorithm that employs deterministic chaos to create a pattern that appears statistically similar to a true random process. PRNGs typically use a starting point or seed. It is common to use the output of a time function to seed a PRNG. There are a multitude of different algorithms for PRGNs, which differ in performance and quality.

One significant weaknesses of a PRNG, is that the output eventually repeats or is periodic. It is also possible, if one knows the algorithm, and the starting point (seed), to recreate the exact same sequence, because digital computers are necessarily deterministic devices. Security mechanisms which rely on PRNGs often have hidden vulnerabilities, due to the PRNGs that they employ.

Certain applications, especially in security, or for example a lottery, require what is, for practical purposes considered a true random number generator. Hardware devices are available, which exploit physical phenomena. Thermal noise (i.e. random motion of electrons), for example, is governed by Quantum Mechanical behaviour and is often used in RNG hardware implementations. Certain quantum mechanical phenomena are generally considered to be random. According to most standard interpretations of Quantum Mechanics, even if you had a complete model of the system (i.e. time evolution of Schrödinger Equation) and knowledge of all the initial variables, to infinite precision, you still would not be able to predict exactly the next outcome of a measurement.

In theory, you could also use radio background noise (think television static or radio noise between the stations). Any of these signal sources can be filtered or used as input themselves for a PRNG to create high quality random sequences, without any noticeable statistical predictability.

Here is very simple toy PRNG algorithm for you to play around with and help understand the concept.

// linear congruential generator
int seed 123456789;

int rand() {
    seed = ( a * seed + c ) % m;
    return seed;
}

The above is taken from Special simple random number generator. I hope that sheds some light on your question.

Community
  • 1
  • 1
Jacques Nel
  • 581
  • 2
  • 11
1

What is the difference between a truly random number and a pseudo random number in C++?

In C++, it is possible to repeat a pseudo random number sequence. A 'truly random' number (I think not supported by C++ nor C, without custom hardware) can not be repeated.


Marked as C++, so here is one C++ approach.

Lately I have been using what C++ provides, but I still control the range of my numbers by shuffling a simple vector of values. This also supports applying the same random shuffle to more than one test, or to repeat a sequence.

typedef std::vector<int> IntVec_t;  // a convenient type

IntVec_t  iVec;                     // a std vector of int

for (int i=1; i<=32; ++i)
   iVec.push_back(i);               // add 32 ints, range 1..32

{        
   std::random_device rd;      // the generator uses this
   std::mt19937_64 gen(rd());  // there are several generators available

   std::shuffle (iVec.begin(), iVec.end(), gen); // shuffle the vector
}

initialized to: ( 16 elements to a line)

 1    2    3    4    5    6    7    8    9   10   11   12   13   14   15   16 

17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32 

Shuffled contents (possible) :

29   26   12   16   20    7   31   28   30   18    4   23   17   14   32    1 

11   13    5    6    9    2   19   10   21   24    3    8   25   27   22   15 
2785528
  • 5,438
  • 2
  • 18
  • 20
0

In short pseudo numbers have repition while

truly random numbers have no repition its have a seed such as a time or anything else which will obviously different each and every time.

  • If you mean repeat by the same number coming up twice, then I don't think that's correct. Truly random numbers from 0-9 will repeat at some point. Only maybe on the second fetch, but certainly on the 10th. If you mean a repeat sequence, then the answer by Jacques Nel already talks about that. – Scratte Apr 27 '20 at 11:41