-
Notifications
You must be signed in to change notification settings - Fork 6
/
Rand.h
executable file
·183 lines (150 loc) · 5.34 KB
/
Rand.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//
// Rand.h - random number generation and related utility functions
//
// Copyright (c) 2013-2016 Arthur Danskin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#ifndef RAND_H
#define RAND_H
#include <random>
const char* thread_current_name();
// #define DEBUG_RAND(X) Reportf X
#define DEBUG_RAND(X)
int& random_seed();
std::mt19937 *&my_random_device();
template <typename T>
inline T randrange()
{
std::uniform_int_distribution<T> uniform_dist(std::numeric_limits<T>::min(),
std::numeric_limits<T>::max());
T result = uniform_dist(*my_random_device());
DEBUG_RAND(("[%s] randrange<%s> %d", thread_current_name(), TYPE_NAME(T), result));
return result;
}
// return a random number from [start-end)
inline int randrange(int start, int end)
{
// ASSERT(start <= end);
if (start >= end-1)
return start;
std::uniform_int_distribution<int> uniform_dist(start, end-1);
int result = uniform_dist(*my_random_device());
DEBUG_RAND(("[%s] randrange(%d, %d) %d", thread_current_name(), start, end, result));
return result;
}
// return a random number from [0-end)
inline int randrange(int end)
{
return randrange(0, end);
}
inline int2 randrange(int2 start, int2 end)
{
return int2(randrange(start.x, end.x), randrange(start.y, end.y));
}
inline int3 randrange(int3 start, int3 end)
{
return int3(randrange(start.x, end.x), randrange(start.y, end.y), randrange(start.z, end.z));
}
inline bool randbool() { return randrange(0, 2); }
inline float randsign() { return randbool() ? 1.f : -1.f; }
template <typename T>
const T &randselect(std::initializer_list<T> l)
{
return *(l.begin() + randrange(0, l.size()));
}
template <typename Vec>
auto randselect(const Vec& vec) -> decltype(*std::begin(vec))
{
const size_t size = std::distance(std::begin(vec), std::end(vec));
if (size == 0)
return dummy_ref<decltype(*std::begin(vec))>();
return *(std::begin(vec) + randrange(0, size));
}
template <typename Vec>
typename Vec::value_type randselect_pop(Vec& vec)
{
if (vec.empty())
return dummy_ref<typename Vec::value_type>();
const int index = randrange(0, vec.size());
typename Vec::value_type val = std::move(vec[index]);
vec_pop(vec, index);
return val;
}
inline float randrange(float start, float end)
{
// ASSERT(start <= end);
if (start == end)
return start;
if (start > end)
swap(start, end);
std::uniform_real_distribution<float> uniform_dist(start, end);
float result = uniform_dist(*my_random_device());
DEBUG_RAND(("[%s] randrange(%g, %g) %g", thread_current_name(), start, end, result));
return result;
}
inline float2 randrange(float2 start, float2 end)
{
return float2(randrange(start.x, end.x), randrange(start.y, end.y));
}
inline float3 randrange(float3 start, float3 end)
{
return float3(randrange(start.x, end.x), randrange(start.y, end.y), randrange(start.z, end.z));
}
inline float randnorm() { return randrange(0.f, 1.f); }
inline float randangle() { return randrange(0.f, M_TAUf); }
inline float2 randpolar(float minradius, float maxradius)
{
return randrange(minradius, maxradius) * angleToVector(randangle());
}
inline float2 randpolar(float radius)
{
return radius * angleToVector(randangle());
}
inline float2 randpolar()
{
return angleToVector(randangle());
}
// randomized vector with length (minradius, maxradius], uniformly distributed throughout area
float2 randpolar_uniform(float minradius, float maxradius) ;
inline bool chance(float v) { return randrange(0.f, 1.f) < v; }
float rand_normal(float mean, float stddev);
// return a random integer from [0-MAXV)
// (truncated) normal distribution, with a mean of 0 and STDDEV (lower numbers more likely)
inline int rand_positive_unormal(uint maxv, float stddev)
{
ASSERT(maxv > 0);
ASSERT(stddev > 0.f);
return clamp(int(floor(fabsf(rand_normal(0, stddev)))), 0, maxv-1);
}
inline float randrange_normal(float mn, float mx)
{
return clamp(rand_normal(0.5f * (mn + mx), 0.25 * (mx-mn)), mn, mx);
}
inline int myrandom_(int mx) { return randrange(mx); }
template <typename V>
inline void vec_shuffle(V& vec)
{
std::random_shuffle(std::begin(vec), std::end(vec), myrandom_);
}
template <typename T>
inline T randlerp(T a, T b)
{
return lerp(a, b, randrange(0.f, 1.f));
}
#endif // RAND_H