Linear congruential generator: Difference between revisions

m
added whitespace and highlighting to make it easier to see which formula is which.
m (added whitespace and highlighting to make it easier to see which formula is which.)
Line 1:
{{task|Randomness}}
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:
 
All linear congruential generators use this formula:
* <math>r_{n + 1} = a \times r_n + c \pmod m</math>
 
Where:
 
Where:
* <math>r_0</math> is a seed.
* <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.
 
 
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 ⟶ 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.
 
BSD formula:
 
;BSD formula:
* <math>state_{n + 1} = 1103515245 \times state_n + 12345 \pmod{2^{31}}</math>
* <math>rand_n = state_n</math>
* <math>rand_n</math> is in range 0 to 2147483647.
 
Microsoft formula:
 
;Microsoft formula:
* <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</math> is in range 0 to 32767.
 
 
The BSD formula was so awful that FreeBSD switched to a different formula. More info is at [[Random number generator (included)#C]].
 
More info is at [[Random number generator (included)#C]].
<br><br>
 
=={{header|360 Assembly}}==