1 /** *********************************************************************** **
2 ** A small "keep it simple and stupid" RNG with some fancy merits:
4 ** Quite platform independent
5 ** Passes ALL dieharder tests! Here *nix sys-rand() e.g. fails miserably:-)
6 ** ~12 times faster than my *nix sys-rand()
7 ** ~4 times faster than SSE2-version of Mersenne twister
8 ** Average cycle length: ~2^126
10 ** Return doubles with a full 53 bit mantissa
13 ** (c) Heinz van Saanen
15 This file is free software: you can redistribute it and/or modify
16 it under the terms of the GNU General Public License as published by
17 the Free Software Foundation, either version 3 of the License, or
18 (at your option) any later version.
20 This file is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 ** *********************************************************************** **/
34 #include <cstdlib> // srand(), rand()
35 #include <ctime> // time()
36 #include "types.h" // (u)int8_t .. (u)int64_t
43 // Keep variables always together
44 struct S { uint64_t a; uint64_t b; uint64_t c; uint64_t d; } s;
46 // Init seed and scramble a few rounds
47 void raninit ( uint64_t seed ) {
48 s.a = 0xf1ea5eed; s.b = s.c = s.d = seed;
49 for ( uint64_t i=0; i<8; i++ ) rand64();
53 // Instance seed random or implicite
54 RKISS() { ::srand ( (uint32_t)time(NULL) ); raninit ( (uint64_t)::rand() ); }
55 // RKISS( uint64_t s ) { raninit ( s ); }
58 // void init ( uint64_t seed ) { raninit ( seed ); }
60 // Return 32 bit unsigned integer in between [0,2^32-1]
61 uint32_t rand32 () { return (uint32_t) rand64 (); }
63 // Return 64 bit unsigned integer in between [0,2^64-1]
65 const uint64_t e = s.a - ((s.b<<7) | (s.b>>57));
66 s.a = s.b ^ ((s.c<<13) | (s.c>>51));
67 s.b = s.c + ((s.d<<37) | (s.d>>27));
72 // Return double in between [0,1). Keep full 53 bit mantissa
73 // double frand () { return (int64_t)(rand64()>>11) * (1.0/(67108864.0*134217728.0)); }