Congruence generator

from Wikipedia, the free encyclopedia

The congruence generators are a class of algorithms that generate random looking sequences of numbers . The numbers generated thereby is called pseudo random numbers because they deterministically generated and thus not really happen to have. Congruence generators are the best known and most widely used recursive arithmetic random number generators .

General congruence generator

A congruence generator is defined by the following parameters:

  • Number of status values
  • module
  • Factors with
  • Increment
  • Start values (not all 0, if )

For now you bet

. Denotes the remainder of the division; see modulo .

The calculated numbers are used as random numbers.

The state of the generator before the generation of is given by the values . This state defines (given ) all subsequent random numbers, since the next random number and the next state are determined by the current state. There are possible states, and therefore an earlier state must be repeated at the latest after taking steps. The congruence generator thus generates a periodic sequence of numbers, the period length can also be significantly smaller than . In the extreme case it is 1, and the generator always generates the same “random number”. When defining the parameters, it is therefore important, among other things, to ensure a sufficient period length.

If you need real random numbers in the interval , you can use the approximation

use if the modulo is large enough to give a sufficiently fine subdivision.

Linear congruence generator

With you get the special case of a linear congruence generator . At it is called a multiplicative congruence generator , and for others it is called a mixed linear congruence generator .

The latter is used more often and has four natural numbers as parameters:

  • module
  • factor
  • Increment
  • Starting value

The further values ​​are then calculated from the start value using the following formula :

The linear congruence generator was introduced by Derrick Henry Lehmer in 1949 . It is used in the runtime libraries of various programming languages to generate pseudo random numbers, e.g. B. in C / C ++ (function rand () in the header file stdlib.h).

In cryptography , however, the linear congruence is not used because it has the parameters of a few values of the number sequence and can calculate and thus the complete sequence of numbers.

Period length

According to Knuth's theorem, linear congruence generators reach their maximum possible period length if the following requirements are met:

  • The increment is relatively prime to the module .
  • Every prime factor of divides .
  • If is divisible by 4, then too .

In this case the generator will generate every number from 0 to exactly once and then start over. So it delivers a pseudo-random permutation of these numbers. The starting value can then be any number from this set.

The multiplicative congruence generator (mit ) must therefore have a period length smaller than . The set of Carmichael says, with given its period length is exactly maximized if:

  • is too coprime
  • is a primitive element modulo .

For some special cases of , the primitive elements can easily be determined modulo :

  • If it is a power of two , then mod 8 must yield the remainder 3 or 5. The period length is then , and the start value must be odd. There are two periods, each half of the odd numbers from to .
  • When a prime number , then must for all prime factors of apply: . Then the period length is . The start value must not be zero.

Defects in the numbers generated

The linear congruence generator does not provide numbers that appear completely random. It can be shown that there is a dependency on consecutive numbers.

Partial period

Often one chooses , whereby the word length of the computer is in bits , because then one does not have to calculate the modulo division explicitly. It arises automatically by cutting off the overflow bits. In this case the least significant bits of the status number behave like a generator with the module . These bits repeat themselves at the latest after steps. This means, in particular, that the least significant bit has period 2 at best, that is, changes regularly between 0 and 1. With the multiplicative congruence generator it is even constant.

In general, the following applies to all linear congruence generators: if is a divisor of the module , then a sequence of numbers with the period results :

a valid: .

If the generator has the period according to Knuth's theorem , then the length of the partial period is exactly for all divisors of .

Because of this subperiod behavior, it is unfavorable to obtain random numbers through if and are not relatively prime . Then, the remainder would be a number that and divides a period of a maximum length run through. If you z. B. wants to simulate a six-sided dice and is even, then returns numbers that are alternately even and odd.

Possible remedy:

  • A mixed linear congruence generator with a period is used , whereby the module is chosen to be coprime. The results are not evenly distributed, but the deviation from the uniform distribution is only small and can often be neglected.
  • One uses a multiplicative congruence generator with period and chooses for a large prime number. Also, if is a multiple of , they are also evenly distributed.
  • A rejection method is set and used with the most significant bits of . The will to bit to the right pushed , with the smallest power of two is . Only those are used , the rest are discarded. This method delivers evenly distributed results.
  • One calculates where the smallest is prime to coprime and is discarded.
  • Some implementations of a linear congruence generator circumvent the problem in that they only supply the more significant part of the state value as a result. E.g. is and the result delivered to the user is with .

Hyperplane Behavior

“Hyperplane behavior” of a linear congruence generator in three dimensions
Mixed congruential: . This generator is used in the
TI-59 from Texas Instruments . Overlapping triples are shown, i.e. h., , , etc. Without overlaps only every third level would be left.

The linear congruence generator exhibits a hyperplane behavior, see Marsaglia's theorem . By suitable choice of the parameters , and , one can optimize the behavior of the generator and achieve a large number of hyperplanes. Given the following rules of thumb , one can build :

  • shouldn't be too big or too small, like:
  • should be chosen as randomly as possible, ie not be a "round" number in dual or decimal representation.
  • With the mixed linear congruence generator, the potency should be as large as possible. It is the minimum value for which is a multiple of . Donald Knuth recommends that the potency should be at least 5. If so , then it should be to get the maximum possible potency .

If you want to be sure that the generator generates good random numbers, you should not rely on these rules of thumb alone, but rather test the generator with the spectral test.

Because of the hyperplane behavior, the inverse congruence generator , which does not have this problem, is occasionally used instead of the linear congruence generator . However, it requires a higher computational effort. It is not a special case of the general congruence generator.

Fibonacci generator

A Fibonacci generator is also a congruence generator (with , and ) and consists of the following components:

  • module
  • Starting values

It should be.

The pseudo random numbers are generated with the following formation rule:

One characteristic is that the cases or never occur. Fibonacci generators are therefore not very suitable as pseudo random number generators. This is especially true for mathematical objects that require more than two random numbers to generate. For example, if you were to try to generate a random point cloud in a cube, all points would be on two levels.

Delayed Fibonacci generator

The principle of the Fibonacci generator can, however, be generalized by not using the last two state values , but rather further back state values ​​to generate the new random number. This results in a lagged Fibonacci generator :

with the starting values

So then is and , the rest are zero. As a rule, one chooses even and and so that the polynomial in

is a primitive polynomial modulo 2 . Then the period length of the generator is at least .

The following table shows some value pairs for and that meet this condition:

A. 2 31 55 73 98 100 135 258 607 3217 23209
B. 1 13 24 25th 27 37 22nd 83 273 576 9739

is a primitive polynomial modulo 2 if and only if this is true for. So you can use instead of always .

This generator is also used in practice. However, it also does not provide any numbers that appear completely random. The problem with the simple Fibonacci generator is only shifted: You never have or . There are also other flaws.

As a remedy, it was suggested to only use consecutive numbers and then discard the next to numbers. This works fine, but at the cost of 5 to 11 times the computational effort. The ranarray generator proposed by Donald Knuth works in this way. He has and , and of 1009 consecutive numbers only ever a block of 100 numbers is used.

To ensure the period , it only depends on the least significant bit in the status values , i.e. on whether they are even or odd. The more significant bits can be modified as desired in order to improve the quality of the random numbers obtained. For example:

Other

The delayed Fibonacci generator can be generalized further by processing more than two state values:

.

is the biggest element in here . In order to guarantee a period of at least , the corresponding polynomial must also be used here

or the equivalent of the polynomial

be a primitive polynomial modulo 2 (with an even module ). A generator constructed in this way with generally delivers better random numbers than with , but again at the price of a higher computational effort.

With a further generalization one can increase the period length given a given and probably also further improve the quality of the random numbers. be a prime factor of . for the calculation rule

are chosen in such a way that the polynomial in

is a primitive polynomial modulo . Then the period length is at least . The previous generator results from this with and as a special case, and delivers a multiplicative congruence generator with the period length .

The polynomial is a primitive polynomial modulo if the following conditions are met:

( )

  • is a primitive element modulo
  • the polynomial is congruent to (modulo )
  • for all prime factors of the degree of the polynomial is positive

Polynomial arithmetic is used (see polynomials and polynomial division ), and the coefficients are calculated modulo (they are elements of the remainder class ring ).

See also

Individual evidence

  1. ^ Donald E. Knuth : The Art of Computer Programming . Volume 2: Seminumerical Algorithms. 3. Edition. Addison-Wesley, 1997, ISBN 0-201-89684-2 , pp. 10-26
  2. StackOverflow forum: Understanding the algorithm of Visual C ++ 's rand () function

Web links

Treatise on bitstream encryptions