| 1 |
#ifndef KISSRANDOM_H |
| 2 |
#define KISSRANDOM_H |
| 3 |
|
| 4 |
#if defined(_MSC_VER) && _MSC_VER == 1500 |
| 5 |
typedef unsigned __int32 uint32_t; |
| 6 |
typedef unsigned __int64 uint64_t; |
| 7 |
#else |
| 8 |
#include <stdint.h> |
| 9 |
#endif |
| 10 |
|
| 11 |
// KISS = "keep it simple, stupid", but high quality random number generator |
| 12 |
// http://www0.cs.ucl.ac.uk/staff/d.jones/GoodPracticeRNG.pdf -> "Use a good RNG and build it into your code" |
| 13 |
// http://mathforum.org/kb/message.jspa?messageID=6627731 |
| 14 |
// https://de.wikipedia.org/wiki/KISS_(Zufallszahlengenerator) |
| 15 |
|
| 16 |
// 32 bit KISS |
| 17 |
struct Kiss32Random { |
| 18 |
uint32_t x; |
| 19 |
uint32_t y; |
| 20 |
uint32_t z; |
| 21 |
uint32_t c; |
| 22 |
|
| 23 |
// seed must be != 0 |
| 24 |
Kiss32Random(uint32_t seed = 123456789) { |
| 25 |
x = seed; |
| 26 |
y = 362436000; |
| 27 |
z = 521288629; |
| 28 |
c = 7654321; |
| 29 |
} |
| 30 |
|
| 31 |
uint32_t kiss() { |
| 32 |
// Linear congruence generator |
| 33 |
x = 69069 * x + 12345; |
| 34 |
|
| 35 |
// Xor shift |
| 36 |
y ^= y << 13; |
| 37 |
y ^= y >> 17; |
| 38 |
y ^= y << 5; |
| 39 |
|
| 40 |
// Multiply-with-carry |
| 41 |
uint64_t t = 698769069ULL * z + c; |
| 42 |
c = t >> 32; |
| 43 |
z = (uint32_t) t; |
| 44 |
|
| 45 |
return x + y + z; |
| 46 |
} |
| 47 |
inline int flip() { |
| 48 |
// Draw random 0 or 1 |
| 49 |
return kiss() & 1; |
| 50 |
} |
| 51 |
inline size_t index(size_t n) { |
| 52 |
// Draw random integer between 0 and n-1 where n is at most the number of data points you have |
| 53 |
return kiss() % n; |
| 54 |
} |
| 55 |
inline void set_seed(uint32_t seed) { |
| 56 |
x = seed; |
| 57 |
} |
| 58 |
}; |
| 59 |
|
| 60 |
// 64 bit KISS. Use this if you have more than about 2^24 data points ("big data" ;) ) |
| 61 |
struct Kiss64Random { |
| 62 |
uint64_t x; |
| 63 |
uint64_t y; |
| 64 |
uint64_t z; |
| 65 |
uint64_t c; |
| 66 |
|
| 67 |
// seed must be != 0 |
| 68 |
Kiss64Random(uint64_t seed = 1234567890987654321ULL) { |
| 69 |
x = seed; |
| 70 |
y = 362436362436362436ULL; |
| 71 |
z = 1066149217761810ULL; |
| 72 |
c = 123456123456123456ULL; |
| 73 |
} |
| 74 |
|
| 75 |
uint64_t kiss() { |
| 76 |
// Linear congruence generator |
| 77 |
z = 6906969069LL*z+1234567; |
| 78 |
|
| 79 |
// Xor shift |
| 80 |
y ^= (y<<13); |
| 81 |
y ^= (y>>17); |
| 82 |
y ^= (y<<43); |
| 83 |
|
| 84 |
// Multiply-with-carry (uint128_t t = (2^58 + 1) * x + c; c = t >> 64; x = (uint64_t) t) |
| 85 |
uint64_t t = (x<<58)+c; |
| 86 |
c = (x>>6); |
| 87 |
x += t; |
| 88 |
c += (x<t); |
| 89 |
|
| 90 |
return x + y + z; |
| 91 |
} |
| 92 |
inline int flip() { |
| 93 |
// Draw random 0 or 1 |
| 94 |
return kiss() & 1; |
| 95 |
} |
| 96 |
inline size_t index(size_t n) { |
| 97 |
// Draw random integer between 0 and n-1 where n is at most the number of data points you have |
| 98 |
return kiss() % n; |
| 99 |
} |
| 100 |
inline void set_seed(uint32_t seed) { |
| 101 |
x = seed; |
| 102 |
} |
| 103 |
}; |
| 104 |
|
| 105 |
#endif |
| 106 |
// vim: tabstop=2 shiftwidth=2 |