Linear congruential generator: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: increased font size for output.)
(<lang julia></lang>)
Line 1,529: Line 1,529:


=={{header|Julia}}==
=={{header|Julia}}==
{{works with|Julia|0.6}}
<tt>lcg_maker</tt> creates a linear congruential generator as a closure. This function is used to create the two generators called for by the task.

<lang Julia>
<tt>getlgc</tt> creates a linear congruential generator as a closure. This function is used to create the two generators called for by the task.
function lcg_maker{T<:Integer}(r::T, a::T, c::T, m::T, sh::T)
<lang julia>function getlgc(r::Integer, a::Integer, c::Integer, m::Integer, sh::Integer)
state = r
state = r
function lcg_rand()
return function lgcrand()
state = mod(a*state + c, m)
state = mod(a * state + c, m)
return state >> sh
return state >> sh
end
end
return lcg_rand
end
end


snum = 10
seed, nrep = 0, 10
bsdrand = getlgc(seed, 1103515245, 12345, 2 ^ 31, 0)
seed = 0
bsd_rand = lcg_maker(seed, 1103515245, 12345, 2^31, 0)


print("The first ", snum, " results for a BSD rand() seeded with ")
println("The first $nrep results for a BSD rand seeded with $seed:")
for _ in 1:nrep
println(seed, ":")
@printf("%14d\n", bsdrand())

for i in 1:snum
println(@sprintf "%14d" bsd_rand())
end
end


ms_rand = lcg_maker(seed, 214013, 2531011, 2^31, 16)
msrand = getlgc(seed, 214013, 2531011, 2 ^ 31, 16)


println("\nThe first $nrep results for a M\$ rand seeded with $seed:")
println()
for _ in 1:nrep
print("The first ", snum, " results for a M\$ rand() seeded with ")
@printf("%14d\n", msrand())
println(seed, ":")
end</lang>

for i in 1:snum
println(@sprintf "%14d" ms_rand())
end
</lang>


{{out}}
{{out}}
<pre>The first 10 results for a BSD rand seeded with 0:
<pre>
The first 10 results for a BSD rand() seeded with 0:
12345
12345
1406932606
1406932606
Line 1,576: Line 1,568:
551188310
551188310


The first 10 results for a M$ rand() seeded with 0:
The first 10 results for a M$ rand seeded with 0:
38
38
7719
7719
Line 1,586: Line 1,578:
32285
32285
10450
10450
30612
30612</pre>
</pre>


=={{header|K}}==
=={{header|K}}==