Pseudo-random numbers/Combined recursive generator MRG32k3a: Difference between revisions
Content added Content deleted
Line 114: | Line 114: | ||
1: 20002 2: 20060 3: 19948 4: 20059 5: 19931 |
1: 20002 2: 20060 3: 19948 4: 20059 5: 19931 |
||
</pre> |
</pre> |
||
=={{header|Phix}}== |
|||
<lang Phix>constant |
|||
-- First generator |
|||
a1 = {0, 1403580, -810728}, |
|||
m1 = power(2,32) - 209, |
|||
-- Second Generator |
|||
a2 = {527612, 0, -1370589}, |
|||
m2 = power(2,32) - 22853, |
|||
d = m1 + 1 |
|||
sequence x1 = {0, 0, 0}, /* list of three last values of gen #1 */ |
|||
x2 = {0, 0, 0} /* list of three last values of gen #2 */ |
|||
procedure seed(integer seed_state) |
|||
assert(seed_state>0 and seed_state<d) |
|||
x1 = {seed_state, 0, 0} |
|||
x2 = {seed_state, 0, 0} |
|||
end procedure |
|||
function next_int() |
|||
atom x1i = mod(a1[1]*x1[1] + a1[2]*x1[2] + a1[3]*x1[3],m1), |
|||
x2i = mod(a2[1]*x2[1] + a2[2]*x2[2] + a2[3]*x2[3],m2) |
|||
x1 = {x1i, x1[1], x1[2]} /* Keep last three */ |
|||
x2 = {x2i, x2[1], x2[2]} /* Keep last three */ |
|||
atom z = mod(x1i-x2i,m1), |
|||
answer = (z + 1) |
|||
return answer |
|||
end function |
|||
function next_float() |
|||
return next_int() / d |
|||
end function |
|||
seed(1234567) |
|||
for i=1 to 5 do |
|||
printf(1,"%d\n",next_int()) |
|||
end for |
|||
seed(987654321) |
|||
sequence r = repeat(0,5) |
|||
for i=1 to 100_000 do |
|||
r[floor(next_float()*5)+1] += 1 |
|||
end for |
|||
?r</lang> |
|||
{{out}} |
|||
<pre> |
|||
1459213977 |
|||
2827710106 |
|||
4245671317 |
|||
3877608661 |
|||
2595287583 |
|||
{20002,20060,19948,20059,19931} |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |