Linear congruential generator: Difference between revisions
Content added Content deleted
(Added Wren) |
(Corrected an error in implementation of Microsoft LCG. Added code to compute ten first values. Added output.) |
||
Line 2,190: | Line 2,190: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
<lang nim>proc bsdRand(seed: int): iterator: int = |
<lang nim>proc bsdRand(seed: int): iterator: int = |
||
var |
var state = seed |
||
result = iterator: int = |
result = iterator: int = |
||
while true: |
while true: |
||
state = (1_103_515_245 * state + 12_345) and 0x7fffffff |
|||
yield |
yield state |
||
proc msvcrtRand(seed: int): iterator: int = |
proc msvcrtRand(seed: int): iterator: int = |
||
var |
var state = seed |
||
result = iterator: int = |
result = iterator: int = |
||
while true: |
while true: |
||
state = (214_013 * state + 2_531_011) and 0x7fffffff |
|||
yield |
yield state shr 16 |
||
echo "BSD with seed = 1 (OEIS A096553):" |
|||
var count = 0 |
|||
let iter1 = bsdRand(1) |
|||
for val in iter1(): |
|||
echo val |
|||
inc count |
|||
if count == 10: |
|||
break |
|||
echo "" |
|||
echo "Microsoft with seed = 0 (OEIS A096558):" |
|||
count = 0 |
|||
let iter2 = msvcrtRand(0) |
|||
for val in iter2(): |
|||
echo val |
|||
inc count |
|||
if count == 10: |
|||
break</lang> |
|||
{{out}} |
|||
<pre>BSD with seed = 1 (OEIS A096553): |
|||
1103527590 |
|||
377401575 |
|||
662824084 |
|||
1147902781 |
|||
2035015474 |
|||
368800899 |
|||
1508029952 |
|||
486256185 |
|||
1062517886 |
|||
267834847 |
|||
Microsoft with seed = 0 (OEIS A096558): |
|||
38 |
|||
7719 |
|||
21238 |
|||
2437 |
|||
8855 |
|||
11797 |
|||
8365 |
|||
32285 |
|||
10450 |
|||
30612</pre> |
|||
=={{header|Oforth}}== |
=={{header|Oforth}}== |