What is the first sequence number in random generator algorithms used in computer programs?

289 Views Asked by At

I am wondering about number generator algorithms which are used in video games and other things that require randomness in computer programs. As far as I understand it's a function that creates a sequence. But you always need to pick a first number to start the sequence. You can save the current number of the sequence in memory. But how to pick a first number and what people usually pick. Is it something like the current date and time measured in nano seconds? Or is just some fixed spot in memory, that is varying a lot? For example some spot in the RAM?

The reason I ask this question in the mathematics section is that I'm not sure if the choice can maintain randomness (or as close to it as possible) in a mathematical sense.

2

There are 2 best solutions below

2
On BEST ANSWER

To address your question as to whether specific choice of seed value can affect the randomness of a RNG, many random generators use a linear congruential sequence in which we derive $X_{n+1}$ from $X_{n}$ by calculating $$X_{n+1} = (aX_n + c)\pmod m$$ where $a$, $c$, and $m$ are selected constants.

This sequence will always be periodic (since there are only $m$ possible values of $X_n$). It is possible in general that different choices of $X_0$ will generate sequences with different periods. However, we can choose $a$, $c$, and $m$ such that the period length is $m$, meaning that every possible choice of $X_0$ must lie in this $m$-cycle. According to Knuth in Vol. 2 of The Art of Computer Programming, a linear congruential sequence has period length if and only if

  1. $c$ and $m$ are relatively prime;
  2. $a-1$ is a multiple of every prime that divides $m$;
  3. if $m$ is a multiple of $4$, then $b$ is a multiple of $4$.

Now we are free to choose $X_0$ to be whatever we want and the sequence will not degenerate into a short cycle. Of course, the sequence still needs to pass other tests for "randomness". For example, the sequence $$1, 2, 3,\ldots, m - 1, 0, 1, 2, \ldots$$ has period length $m$ but we would hesitate to call it random. There are already constants that are widely known to generate quite good sequences (passing a wide array of statistical and empirical tests), so if you're just worrying about choice of seed, you're right that the current date and time is quite safe. If a program is run multiple times, starting $X_0$ at the value attained by $X_n$ in the last run is also quite safe.

0
On

All of your suggestions are used, depending on circumstances. In some programming languages (for example, in C), a fixed starting point is used, meaning the two different runs of the program will generate exactly the same sequence of "random" numbers. Whether that's good or bad depends on circumstances - if you want to debug the code it's good, if you want to generate exam questions it's bad. In the latter case, to avoid predictable questions, the programmer would have to manually call a function that initializes the (pseudo) random number generator (RNG) to a different value, often based on the current time as you suspect.

Some operating systems also allow you access to (almost) truly random numbers, which are usually obtained from either dedicated hardware that measures e.g. thermal noise, or from outside events such as keystrokes, network packets received, and so on (of course, depending on the choice of source, these numbers might also not be truly random in a mathematical sense. But they are at least highly non-deterministic) . Since these truly random numbers are usually generated rather slowly (you need to wait for events to happen), they are typically also not used as random numbers directly, but instead to (periodically) initialize a (pseudo) RNG.

Mathematically speaking, the sequence of a (pseudo) RNG, whether initialized with a known or a truly random numbers, is never truly random. Even if the RNG has a larger internal state space than the random numbers it outputs, the state space is still finite, so at some point it will hit a state it was already in before, and keep repeating the same sequence of numbers from that point on. But usually this period is made so huge that the numbers are "random enough" for all practical purposes, if the RNG is designed carefully.

For more information on (pseudo) RNGs, their properties, and ways to test how random the numbers they generate are, see for example here