NDEVR
API Documentation
Random.h
1/*--------------------------------------------------------------------------------------------
2Copyright (c) 2019, NDEVR LLC
3tyler.parke@ndevr.org
4 __ __ ____ _____ __ __ _______
5 | \ | | | __ \ | ___|\ \ / / | __ \
6 | \ | | | | \ \ | |___ \ \ / / | |__) |
7 | . \| | | |__/ / | |___ \ V / | _ /
8 | |\ |_|_____/__|_____|___\_/____| | \ \
9 |__| \__________________________________| \__\
10
11Subject to the terms of the Enterprise+ Agreement, NDEVR hereby grants
12Licensee a limited, non-exclusive, non-transferable, royalty-free license
13(without the right to sublicense) to use the API solely for the purpose of
14Licensee's internal development efforts to develop applications for which
15the API was provided.
16
17The above copyright notice and this permission notice shall be included in all
18copies or substantial portions of the Software.
19
20THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
21INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
22PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
23FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
24OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25DEALINGS IN THE SOFTWARE.
26
27Library: Base
28File: Random
29Included in API: True
30Author(s): Tyler Parke
31 *-----------------------------------------------------------------------------------------**/
32#pragma once
33#include <NDEVR/BaseValues.h>
34#include <random>
35namespace NDEVR
36{
40 class Random
41 {};
42
54 template<class t_type>
55 t_type MakeRandom(t_type max)
56 {
57 static thread_local std::mt19937 rng(std::random_device{}());
58 std::uniform_int_distribution<t_type> gen(0, max);
59 return gen(rng);
60 };
61
74 template<>
76 {
77 return (max * cast<fltp04>(MakeRandom<uint02>(Constant<uint02>::Max))) / Constant<uint02>::Max;
78 };
79
80
93 template<>
95 {
96 return (max * MakeRandom<uint04>(Constant<uint04>::Max)) / cast<fltp08>(Constant<uint04>::Max);
97 }
98
111
112 template<class t_type>
113 t_type MakeRandom(t_type min, t_type max)
114 {
115 lib_assert(min <= max, "Random generation min value must be less than or equal to max");
116 return cast<t_type>(MakeRandom<fltp08>(cast<fltp08>(max - min))) + min;
117 };
118
122 template<sint04 ia = 16807, sint04 im = 2147483647, sint04 iq = 127773, sint04 ir = 2836, sint04 ntab = 32>
124 {
125 public:
131 : m_idum(didum > 0 ? -didum : didum)
132 , m_eps(1.2e-7)
133 {}
134
139 {
140 return gasdev(m_idum);
141 }
142 protected:
149 {
150 fltp08 am = 1.0 / im;
151 fltp08 ntiv = (1 + (im - 1) / ntab);
152 sint04 j;
153 sint08 k;
154
155 fltp08 temp;
156
157 if (idum <= 0 || !iy)
158 {
159 if (-idum < 1)
160 idum = 1;
161 else
162 idum = -(idum);
163 for (j = ntab + 7; j >= 0; j--)
164 {
165 k = (idum) / iq;
166 idum = ia * (idum - k * iq) - ir * k;
167 if (idum < 0)
168 idum += im;
169 if (j < ntab)
170 iv[j] = idum;
171 }
172 iy = iv[0];
173 }
174 k = (idum) / iq;
175 idum = ia * (idum - k * iq) - ir * k;
176 if (idum < 0)
177 idum += im;
178 j = iy / ntiv;
179 iy = iv[j];
180 iv[j] = idum;
181 if ((temp = am * iy) > 1 - m_eps)
182 return m_eps;
183 else
184 return temp;
185 }
186
192 {
193 double fac, rsq, v1, v2;
194
195 if (iset == 0)
196 {
197 do
198 {
199 v1 = 2.0 * ran1(idum) - 1.0;
200 v2 = 2.0 * ran1(idum) - 1.0;
201 rsq = v1*v1 + v2 * v2;
202 }
203 while (rsq >= 1.0 || rsq == 0.0);
204
205 fac = sqrt(-2.0 * log(rsq) / rsq);
206 gset = v1 * fac;
207 iset = 1;
208 return v2 * fac;
209 }
210 else
211 {
212 iset = 0;
213 return gset;
214 }
215 }
216 private:
217 sint08 iset = 0;
218 fltp08 gset;
219 sint08 m_idum;
220 fltp08 m_eps;
221 sint08 iy = 0;
222 sint08 iv[ntab];
223 GaussianRN(const GaussianRN& rhs) = delete;
224 const GaussianRN &operator=(const GaussianRN& rhs) = delete;
225 };
226
227
228
229}
Used to generate a random number over a Gaussian distribution with templated setup parameters for ver...
Definition Random.h:124
fltp08 get()
Generates the next Gaussian-distributed random number.
Definition Random.h:138
GaussianRN(sint08 didum)
Constructs a Gaussian random number generator with the given seed.
Definition Random.h:130
fltp08 gasdev(sint08 &idum)
Generates a Gaussian-distributed random number using the Box-Muller transform.
Definition Random.h:191
fltp08 ran1(sint08 &idum)
Generates a uniform random number in the range [0, 1) using a Park-Miller minimal standard generator.
Definition Random.h:148
Dummy class for including random number generator functions.
Definition Random.h:41
The primary namespace for the NDEVR SDK.
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
t_type MakeRandom(t_type max)
Returns a pseudo-random number between 0 and the associated max value.
Definition Random.h:55
double fltp08
Defines an alias representing an 8 byte floating-point number.
int32_t sint04
-Defines an alias representing a 4 byte, signed integer.
t_type sqrt(const t_type &value)
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
constexpr t_to cast(const Angle< t_from > &value)
Casts an Angle from one backing type to another.
Definition Angle.h:408