-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathRandom.h
110 lines (92 loc) · 2.27 KB
/
Random.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once
#include "Types.h"
#include <sys/types.h>
#include <unistd.h>
//-----------------------------------------------------------------------------
// Xorshift RNG, based on code by George Marsaglia
// Actual implementation by Sebastiano Vigna
// See:
// http://vigna.di.unimi.it/ftp/papers/xorshift.pdf
// http://en.wikipedia.org/wiki/Xorshift
#define RANDSTATES 16
struct Rand
{
uint64_t state[RANDSTATES];
uint32_t *state32;
int p;
Rand()
{
state32= (uint32_t *)state;
reseed(0);
}
Rand( uint64_t seed )
{
state32= (uint32_t *)state;
reseed(seed);
}
void reseed ( uint64_t seed )
{
int i = 0;
state[i++]= seed;
if (!seed) state[i++] = seed = 0x0139408dcbbf7a44ULL;
do {
seed ^= seed << 13;
seed ^= seed >> 7;
seed ^= seed << 17;
state[i++] = seed;
} while ( i < RANDSTATES );
_reseed();
}
void _reseed() {
p= 0;
for(int i = 0; i < 1000; i++)
mix();
}
//-----------------------------------------------------------------------------
void mix ( void )
{
const uint64_t s0 = state[p];
uint64_t s1 = state[p = (p + 1) % RANDSTATES];
s1 ^= s1 << 31; // a
state[p] = s1 ^ s0 ^ (s1 >> 11) ^ (s0 >> 30); // b, c
}
uint32_t rand_u32 ( void )
{
return uint32_t(rand_u64() & 0xFFFFFFFF);
}
uint64_t rand_u64 ( void )
{
mix();
uint64_t v = state[p] * 0x106689d45497fdb5ULL;
return v;
}
void rand_p ( void * blob, int bytes )
{
uint64_t * blocks = reinterpret_cast<uint64_t*>(blob);
while(bytes >= 8)
{
blocks[0] = rand_u64();
blocks++;
bytes -= 8;
}
if (bytes) {
uint8_t * tail = reinterpret_cast<uint8_t*>(blocks);
uint64_t last = rand_u64();
for(int i = 0; i < bytes; i++)
{
tail[i] = uint8_t(last & 0xFF);
last = last >> 8;
}
}
}
};
//-----------------------------------------------------------------------------
extern Rand g_rand1;
inline uint32_t rand_u32 ( void ) { return g_rand1.rand_u32(); }
inline uint64_t rand_u64 ( void ) { return g_rand1.rand_u64(); }
inline void rand_p ( void * blob, int bytes )
{
g_rand1.rand_p(blob, bytes);
}
//-----------------------------------------------------------------------------
/* vim: set sts=2 sw=2 et: */