RosettaCodeData/Task/Linear-congruential-generator/D/linear-congruential-generator.d

29 lines
594 B
D
Raw Permalink Normal View History

2013-04-10 21:29:02 -07:00
struct LinearCongruentialGenerator {
enum uint RAND_MAX = (1U << 31) - 1;
uint seed = 0;
2015-02-20 00:35:01 -05:00
uint randBSD() pure nothrow @nogc {
2013-04-10 21:29:02 -07:00
seed = (seed * 1_103_515_245 + 12_345) & RAND_MAX;
return seed;
}
2015-02-20 00:35:01 -05:00
uint randMS() pure nothrow @nogc {
2013-04-10 21:29:02 -07:00
seed = (seed * 214_013 + 2_531_011) & RAND_MAX;
return seed >> 16;
}
}
void main() {
2015-02-20 00:35:01 -05:00
import std.stdio;
2013-04-10 21:29:02 -07:00
LinearCongruentialGenerator rnd;
2015-02-20 00:35:01 -05:00
foreach (immutable i; 0 .. 10)
writeln(rnd.randBSD);
writeln;
2013-04-10 21:29:02 -07:00
rnd.seed = 0;
2015-02-20 00:35:01 -05:00
foreach (immutable i; 0 .. 10)
writeln(rnd.randMS);
2013-04-10 21:29:02 -07:00
}