Jump to content

Linear congruential generator: Difference between revisions

<lang julia></lang>
m (→‎{{header|REXX}}: increased font size for output.)
(<lang julia></lang>)
Line 1,529:
 
=={{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>lcg_makergetlgc</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
return function lcg_randlgcrand()
state = mod(a * state + c, m)
return state >> sh
end
return lcg_rand
end
 
snumseed, nrep = 0, 10
bsd_randbsdrand = lcg_makergetlgc(seed, 1103515245, 12345, 2 ^ 31, 0)
seed = 0
bsd_rand = lcg_maker(seed, 1103515245, 12345, 2^31, 0)
 
printprintln("The first ", snum, "$nrep results for a BSD rand() seeded with $seed:")
for i_ in 1:snumnrep
println(seed, ":")
@printf("%14d\n", bsdrand())
 
for i in 1:snum
println(@sprintf "%14d" bsd_rand())
end
 
ms_randmsrand = lcg_makergetlgc(seed, 214013, 2531011, 2 ^ 31, 16)
 
printprintln("The\nThe first ", snum, "$nrep results for a M\$ rand() seeded with $seed:")
println()
for i_ in 1:snumnrep
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}}
<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
1406932606
Line 1,576 ⟶ 1,568:
551188310
 
The first 10 results for a M$ rand() seeded with 0:
38
7719
Line 1,586 ⟶ 1,578:
32285
10450
30612</pre>
</pre>
 
=={{header|K}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.