145
|
1 /**
|
|
2 * Random number generators for internal usage.
|
|
3 *
|
|
4 * Copyright: Copyright Digital Mars 2014.
|
|
5 * License: $(WEB www.boost.org/LICENSE_1_0.txt, Boost License 1.0).
|
|
6 */
|
|
7 module rt.util.random;
|
|
8
|
|
9 struct Rand48
|
|
10 {
|
|
11 private ulong rng_state;
|
|
12
|
|
13 @safe @nogc nothrow:
|
|
14
|
|
15 void defaultSeed()
|
|
16 {
|
|
17 import ctime = core.stdc.time : time;
|
|
18 seed(cast(uint)ctime.time(null));
|
|
19 }
|
|
20
|
|
21 pure:
|
|
22
|
|
23 void seed(uint seedval)
|
|
24 {
|
|
25 assert(seedval);
|
|
26 rng_state = cast(ulong)seedval << 16 | 0x330e;
|
|
27 popFront();
|
|
28 }
|
|
29
|
|
30 auto opCall()
|
|
31 {
|
|
32 auto result = front;
|
|
33 popFront();
|
|
34 return result;
|
|
35 }
|
|
36
|
|
37 @property uint front()
|
|
38 {
|
|
39 return cast(uint)(rng_state >> 16);
|
|
40 }
|
|
41
|
|
42 void popFront()
|
|
43 {
|
|
44 immutable ulong a = 25214903917;
|
|
45 immutable ulong c = 11;
|
|
46 immutable ulong m_mask = (1uL << 48uL) - 1;
|
|
47 rng_state = (a*rng_state+c) & m_mask;
|
|
48 }
|
|
49
|
|
50 enum empty = false;
|
|
51 }
|