-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimplerandom.h
More file actions
93 lines (75 loc) · 2.29 KB
/
simplerandom.h
File metadata and controls
93 lines (75 loc) · 2.29 KB
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
#ifndef RANDOM_SIMPLE_RANDOM
#define RANDOM_SIMPLE_RANDOM
namespace simplerandom
{
struct uint2
{
unsigned int x;
unsigned int y;
};
typedef struct RandomGenT
{
uint2 state;
unsigned int maxNumbers;
unsigned int lazy; // or dummy to have uint4 generator data.
} RandomGen;
static inline unsigned int NextState(RandomGen *gen)
{
const unsigned int x = (gen->state).x * 17 + (gen->state).y * 13123;
(gen->state).x = (x << 13) ^ x;
(gen->state).y ^= (x << 7);
return x;
}
static inline RandomGen RandomGenInit(const int a_seed)
{
RandomGen gen;
gen.state.x = (a_seed * (a_seed * a_seed * 15731 + 74323) + 871483);
gen.state.y = (a_seed * (a_seed * a_seed * 13734 + 37828) + 234234);
gen.lazy = 0;
for (int i = 0; i < (a_seed % 7); i++)
NextState(&gen);
return gen;
}
static inline float rndFloat(RandomGen *gen)
{
const unsigned int x = NextState(gen);
const unsigned int tmp = (x * (x * x * 15731 + 74323) + 871483);
const float scale = (1.0f / 4294967296.0f);
return ((float) (tmp)) * scale;
}
static inline float rnd(RandomGen& gen, float s, float e)
{
const float t = rndFloat(&gen);
return s + t*(e - s);
}
static inline unsigned int rand(RandomGen& gen)
{
return NextState(&gen);
}
// static inline float4 rndFloat4_Pseudo(RandomGen *gen)
// {
// unsigned int x = NextState(gen);
//
// const unsigned int x1 = (x * (x * x * 15731 + 74323) + 871483);
// const unsigned int y1 = (x * (x * x * 13734 + 37828) + 234234);
// const unsigned int z1 = (x * (x * x * 11687 + 26461) + 137589);
// const unsigned int w1 = (x * (x * x * 15707 + 789221) + 1376312589);
//
// const float scale = (1.0f / 4294967296.0f);
//
// return make_float4((float) (x1), (float) (y1), (float) (z1), (float) (w1)) * scale;
// }
//
// static inline float2 rndFloat2_Pseudo(RandomGen *gen)
// {
// unsigned int x = NextState(gen);
//
// const unsigned int x1 = (x * (x * x * 15731 + 74323) + 871483);
// const unsigned int y1 = (x * (x * x * 13734 + 37828) + 234234);
//
// const float scale = (1.0f / 4294967296.0f);
//
// return make_float2((float) (x1), (float) (y1)) * scale;
// }
};
#endif