Linear congruential generator: Difference between revisions
Content added Content deleted
m (added whitespace and highlighting to make it easier to see which formula is which.) |
|||
Line 1: | Line 1: | ||
{{task|Randomness}} |
{{task|Randomness}} |
||
The [[wp:linear congruential generator|linear congruential generator]] is a very simple example of a [[random number generator]]. |
The [[wp:linear congruential generator|linear congruential generator]] is a very simple example of a [[random number generator]]. |
||
All linear congruential generators use this formula: |
|||
* <math>r_{n + 1} = a \times r_n + c \pmod m</math> |
* <math>r_{n + 1} = a \times r_n + c \pmod m</math> |
||
⚫ | |||
⚫ | |||
* <math>r_0</math> is a seed. |
* <math>r_0</math> is a seed. |
||
* <math>r_1</math>, <math>r_2</math>, <math>r_3</math>, ..., are the random numbers. |
* <math>r_1</math>, <math>r_2</math>, <math>r_3</math>, ..., are the random numbers. |
||
* <math>a</math>, <math>c</math>, <math>m</math> are constants. |
* <math>a</math>, <math>c</math>, <math>m</math> are constants. |
||
If one chooses the values of <math>a</math>, <math>c</math> and <math>m</math> with care, then the generator produces a uniform distribution of integers from <math>0</math> to <math>m - 1</math>. |
If one chooses the values of <math>a</math>, <math>c</math> and <math>m</math> with care, then the generator produces a uniform distribution of integers from <math>0</math> to <math>m - 1</math>. |
||
Line 18: | Line 20: | ||
In these formulas, the seed becomes <math>state_0</math>. The random sequence is <math>rand_1</math>, <math>rand_2</math> and so on. |
In these formulas, the seed becomes <math>state_0</math>. The random sequence is <math>rand_1</math>, <math>rand_2</math> and so on. |
||
⚫ | |||
⚫ | |||
* <math>state_{n + 1} = 1103515245 \times state_n + 12345 \pmod{2^{31}}</math> |
* <math>state_{n + 1} = 1103515245 \times state_n + 12345 \pmod{2^{31}}</math> |
||
* <math>rand_n = state_n</math> |
* <math>rand_n = state_n</math> |
||
* <math>rand_n</math> is in range 0 to 2147483647. |
* <math>rand_n</math> is in range 0 to 2147483647. |
||
⚫ | |||
⚫ | |||
* <math>state_{n + 1} = 214013 \times state_n + 2531011 \pmod{2^{31}}</math> |
* <math>state_{n + 1} = 214013 \times state_n + 2531011 \pmod{2^{31}}</math> |
||
* <math>rand_n = state_n \div 2^{16}</math> |
* <math>rand_n = state_n \div 2^{16}</math> |
||
* <math>rand_n</math> is in range 0 to 32767. |
* <math>rand_n</math> is in range 0 to 32767. |
||
The BSD formula was so awful that FreeBSD switched to a different formula. |
The BSD formula was so awful that FreeBSD switched to a different formula. |
||
More info is at [[Random number generator (included)#C]]. |
|||
<br><br> |
|||
=={{header|360 Assembly}}== |
=={{header|360 Assembly}}== |