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>
35/**--------------------------------------------------------------------------------------------------
36Namespace: Parke
37Namespace that wraps all Logic created by Tyler Parke
38 *-----------------------------------------------------------------------------------------------**/
39
40namespace NDEVR
41{
42 class Random
43 {};
44 /**--------------------------------------------------------------------------------------------------
45 Fn: t_type getRandom(t_type max)
46
47 \brief Returns a pseudo-random number between 0 and the associated max value
48
49 Author: Tyler Parke
50
51 Date: 2017-11-18
52
53 Parameters:
54 max - The maximum value the random value can take on.
55
56 Returns: A pseudo-random number.
57 *-----------------------------------------------------------------------------------------------**/
58 template<class t_type>
59 t_type getRandom(t_type max)
60 {
61 static std::mt19937 rng(rand());
62 std::uniform_int_distribution<t_type> gen(0, max);
63 return gen(rng);
64 };
65
66 /**--------------------------------------------------------------------------------------------------
67 Fn: fltp04 getRandom(fltp04 max)
68
69 Returns a pseudo-random number between 0 and the associated max value
70
71 Author: Tyler Parke
72
73 Date: 2017-11-18
74
75 Parameters:
76 max - The maximum value the random value can take on.
77
78 Returns: A pseudo-random number.
79 *-----------------------------------------------------------------------------------------------**/
80 template<>
85
86
87 /**--------------------------------------------------------------------------------------------------
88 Fn: fltp08 getRandom(fltp08 max)
89
90 \brief Returns a pseudo-random number between 0 and the associated max value
91
92 Author: Tyler Parke
93
94 Date: 2017-11-18
95
96 Parameters:
97 max - The maximum value the random value can take on.
98
99 Returns: A pseudo-random number.
100 *-----------------------------------------------------------------------------------------------**/
101 template<>
106
107 /**--------------------------------------------------------------------------------------------------
108 Fn: template<class t_type> t_type getRandom(t_type max, t_type min)
109
110 \brief Returns a pseudo-random number between 0 and the associated max value
111
112 Author: Tyler Parke
113
114 Date: 2017-11-18
115
116 Parameters:
117 max - The maximum value the random value can take on.
118 min - The maximum value the random value can take on.
119 Returns: A pseudo-random number.
120 *-----------------------------------------------------------------------------------------------**/
121
122 template<class t_type>
123 t_type getRandom(t_type min, t_type max)
124 {
125 lib_assert(min <= max, "Random generation min value must be less than or equal to max");
126 return cast<t_type>(getRandom<fltp08>(cast<fltp08>(max - min))) + min;
127 };
128
129 template<sint04 ia = 16807, sint04 im = 2147483647, sint04 iq = 127773, sint04 ir = 2836, sint04 ntab = 32>
131 {
132 public:
134 : idum(didum)
135 , eps(1.2e-7)
136 {}
138 {
139 return gasdev(idum);
140 }
141 protected:
143 {
144 fltp08 am = 1.0 / im;
145 fltp08 ntiv = (1 + (im - 1) / ntab);
146 sint04 j;
147 sint08 k;
148 static sint08 iy = 0;
149 static sint08 iv[ntab];
150 fltp08 temp;
151
152 if (idum <= 0 || !iy)
153 {
154 if (-idum < 1)
155 idum = 1;
156 else
157 idum = -(idum);
158 for (j = ntab + 7; j >= 0; j--)
159 {
160 k = (idum) / iq;
161 idum = ia * (idum - k * iq) - ir * k;
162 if (idum < 0)
163 idum += im;
164 if (j < ntab)
165 iv[j] = idum;
166 }
167 iy = iv[0];
168 }
169 k = (idum) / iq;
170 idum = ia * (idum - k * iq) - ir * k;
171 if (idum < 0)
172 idum += im;
173 j = iy / ntiv;
174 iy = iv[j];
175 iv[j] = idum;
176 if ((temp = am * iy) > 1 - eps)
177 return eps;
178 else
179 return temp;
180 }
182 {
183 static sint08 iset = 0;
184 static fltp08 gset;
185 double fac, rsq, v1, v2;
186
187 if (iset == 0)
188 {
189 do
190 {
191 v1 = 2.0 * ran1(idum) - 1.0;
192 v2 = 2.0 * ran1(idum) - 1.0;
193 rsq = v1*v1 + v2 * v2;
194 }
195 while (rsq >= 1.0 || rsq == 0.0);
196
197 fac = sqrt(-2.0 * log(rsq) / rsq);
198 gset = v1 * fac;
199 iset = 1;
200 return v2 * fac;
201 }
202 else
203 {
204 iset = 0;
205 return gset;
206 }
207 }
208 private:
209 sint08 idum;
210 fltp08 eps;
211 GaussianRN(const GaussianRN& rhs) = delete;
212 const GaussianRN &operator=(const GaussianRN& rhs) = delete;
213 };
214
215
216
217}
#define lib_assert(expression, message)
Asserts some logic in the code. Disabled in non debug mode by default. Can be re-enabled in release u...
Definition LibAssert.h:70
Definition Random.h:131
fltp08 gasdev()
Definition Random.h:137
fltp08 ran1(sint08 &idum)
Definition Random.h:142
fltp08 gasdev(sint08 &idum)
Definition Random.h:181
GaussianRN(sint08 didum)
Definition Random.h:133
Definition Random.h:43
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:76
int64_t sint08
-Defines an alias representing an 8 byte, signed integer -Can represent exact integer values -9223372...
Definition BaseValues.hpp:86
float fltp04
Defines an alias representing a 4 byte floating-point number.
Definition BaseValues.hpp:157
t_type getRandom(t_type max)
Returns a pseudo-random number between 0 and the associated max value.
Definition Random.h:59
t_type sqrt(const t_type &value)
Definition VectorFunctions.hpp:1309
constexpr t_to cast(const Angle< t_from > &value)
Definition Angle.h:514
double fltp08
Defines an alias representing an 8 byte floating-point number.
Definition BaseValues.hpp:181
Definition BaseValues.hpp:272