API Documentation
Loading...
Searching...
No Matches
Random.h
Go to the documentation of this file.
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{
37 /**--------------------------------------------------------------------------------------------------
38 \brief Dummy class for including random number generator functions
39 **/
40 class Random
41 {};
42 /**--------------------------------------------------------------------------------------------------
43 \brief Returns a pseudo-random number between 0 and the associated max value
44
45 Author: Tyler Parke
46
47 Date: 2017-11-18
48
49 Parameters:
50 max - The maximum value the random value can take on.
51
52 \returns A pseudo-random number.
53 **/
54 template<class t_type>
55 t_type MakeRandom(t_type max)
56 {
57 static std::mt19937 rng(rand());
58 std::uniform_int_distribution<t_type> gen(0, max);
59 return gen(rng);
60 };
61
62 /**--------------------------------------------------------------------------------------------------
63 Returns a pseudo-random number between 0 and the associated max value
64
65 Author: Tyler Parke
66
67 Date: 2017-11-18
68
69 Parameters:
70 max - The maximum value the random value can take on.
71
72 \returns A pseudo-random number.
73 **/
74 template<>
79
80
81 /**--------------------------------------------------------------------------------------------------
82 \brief Returns a pseudo-random number between 0 and the associated max value
83
84 Author: Tyler Parke
85
86 Date: 2017-11-18
87
88 Parameters:
89 max - The maximum value the random value can take on.
90
91 \returns A pseudo-random number.
92 **/
93 template<>
98
99 /**--------------------------------------------------------------------------------------------------
100 \brief Returns a pseudo-random number between 0 and the associated max value
101
102 Author: Tyler Parke
103
104 Date: 2017-11-18
105
106 Parameters:
107 max - The maximum value the random value can take on.
108 min - The maximum value the random value can take on.
109 \returns A pseudo-random number.
110 **/
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 /**--------------------------------------------------------------------------------------------------
119 \brief Used to generate a random number over a Gaussian distribution with templated setup parameters
120 for very quick speed.
121 **/
122 template<sint04 ia = 16807, sint04 im = 2147483647, sint04 iq = 127773, sint04 ir = 2836, sint04 ntab = 32>
124 {
125 public:
127 : idum(didum)
128 , eps(1.2e-7)
129 {}
131 {
132 return gasdev(idum);
133 }
134 protected:
136 {
137 fltp08 am = 1.0 / im;
138 fltp08 ntiv = (1 + (im - 1) / ntab);
139 sint04 j;
140 sint08 k;
141 static sint08 iy = 0;
142 static sint08 iv[ntab];
143 fltp08 temp;
144
145 if (idum <= 0 || !iy)
146 {
147 if (-idum < 1)
148 idum = 1;
149 else
150 idum = -(idum);
151 for (j = ntab + 7; j >= 0; j--)
152 {
153 k = (idum) / iq;
154 idum = ia * (idum - k * iq) - ir * k;
155 if (idum < 0)
156 idum += im;
157 if (j < ntab)
158 iv[j] = idum;
159 }
160 iy = iv[0];
161 }
162 k = (idum) / iq;
163 idum = ia * (idum - k * iq) - ir * k;
164 if (idum < 0)
165 idum += im;
166 j = iy / ntiv;
167 iy = iv[j];
168 iv[j] = idum;
169 if ((temp = am * iy) > 1 - eps)
170 return eps;
171 else
172 return temp;
173 }
175 {
176 static sint08 iset = 0;
177 static fltp08 gset;
178 double fac, rsq, v1, v2;
179
180 if (iset == 0)
181 {
182 do
183 {
184 v1 = 2.0 * ran1(idum) - 1.0;
185 v2 = 2.0 * ran1(idum) - 1.0;
186 rsq = v1*v1 + v2 * v2;
187 }
188 while (rsq >= 1.0 || rsq == 0.0);
189
190 fac = sqrt(-2.0 * log(rsq) / rsq);
191 gset = v1 * fac;
192 iset = 1;
193 return v2 * fac;
194 }
195 else
196 {
197 iset = 0;
198 return gset;
199 }
200 }
201 private:
202 sint08 idum;
203 fltp08 eps;
204 GaussianRN(const GaussianRN& rhs) = delete;
205 const GaussianRN &operator=(const GaussianRN& rhs) = delete;
206 };
207
208
209
210}
#define lib_assert(expression, message)
Definition LibAssert.h:61
Used to generate a random number over a Gaussian distribution with templated setup parameters for ver...
Definition Random.h:124
fltp08 gasdev()
Definition Random.h:130
fltp08 ran1(sint08 &idum)
Definition Random.h:135
fltp08 gasdev(sint08 &idum)
Definition Random.h:174
GaussianRN(sint08 didum)
Definition Random.h:126
Dummy class for including random number generator functions.
Definition Random.h:41
Definition ACIColor.h:37
int32_t sint04
-Defines an alias representing a 4 byte, signed integer. -Can represent exact integer values -2147483...
Definition BaseValues.hpp:64
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:71
t_type MakeRandom(t_type max)
Returns a pseudo-random number between 0 and the associated max value.
Definition Random.h:55
float fltp04
Defines an alias representing a 4 byte floating-point number Bit layout is as follows: -Sign: 1 bit a...
Definition BaseValues.hpp:127
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1225
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:375
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:149
Defines for a given type (such as sint04, fltp08, UUID, etc) a maximum, minimum, and reserved 'invali...
Definition BaseValues.hpp:233