xref: /netbsd-src/external/gpl3/gcc.old/dist/libphobos/libdruntime/rt/util/random.d (revision 627f7eb200a4419d89b531d55fccd2ee3ffdcde0)
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 
defaultSeedRand4815     void defaultSeed()
16     {
17         import ctime = core.stdc.time : time;
18         seed(cast(uint)ctime.time(null));
19     }
20 
21 pure:
22 
seedRand4823     void seed(uint seedval)
24     {
25         assert(seedval);
26         rng_state = cast(ulong)seedval << 16 | 0x330e;
27         popFront();
28     }
29 
opCallRand4830     auto opCall()
31     {
32         auto result = front;
33         popFront();
34         return result;
35     }
36 
frontRand4837     @property uint front()
38     {
39         return cast(uint)(rng_state >> 16);
40     }
41 
popFrontRand4842     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 }
52