RosettaCodeData/Task/Linear-congruential-generator/Julia/linear-congruential-generator.julia

25 lines
584 B
Text
Raw Permalink Normal View History

2020-02-17 23:21:07 -08:00
using Printf
2018-06-22 20:57:24 +00:00
function getlgc(r::Integer, a::Integer, c::Integer, m::Integer, sh::Integer)
2015-11-18 06:14:39 +00:00
state = r
2018-06-22 20:57:24 +00:00
return function lgcrand()
state = mod(a * state + c, m)
2015-11-18 06:14:39 +00:00
return state >> sh
end
end
2018-06-22 20:57:24 +00:00
seed, nrep = 0, 10
bsdrand = getlgc(seed, 1103515245, 12345, 2 ^ 31, 0)
2015-11-18 06:14:39 +00:00
2018-06-22 20:57:24 +00:00
println("The first $nrep results for a BSD rand seeded with $seed:")
for _ in 1:nrep
@printf("%14d\n", bsdrand())
2015-11-18 06:14:39 +00:00
end
2018-06-22 20:57:24 +00:00
msrand = getlgc(seed, 214013, 2531011, 2 ^ 31, 16)
2015-11-18 06:14:39 +00:00
2018-06-22 20:57:24 +00:00
println("\nThe first $nrep results for a M\$ rand seeded with $seed:")
for _ in 1:nrep
@printf("%14d\n", msrand())
2015-11-18 06:14:39 +00:00
end