Ocean
Random.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) Meta Platforms, Inc. and affiliates.
3  *
4  * This source code is licensed under the MIT license found in the
5  * LICENSE file in the root directory of this source tree.
6  */
7 
8 #ifndef META_OCEAN_MATH_RANDOM_H
9 #define META_OCEAN_MATH_RANDOM_H
10 
11 #include "ocean/math/Math.h"
12 
13 #include "ocean/math/Euler.h"
15 #include "ocean/math/Numeric.h"
16 #include "ocean/math/Quaternion.h"
17 #include "ocean/math/Rotation.h"
19 #include "ocean/math/Vector2.h"
20 #include "ocean/math/Vector3.h"
21 #include "ocean/math/Vector4.h"
22 
24 #include "ocean/base/RandomI.h"
25 
26 namespace Ocean
27 {
28 
29 template <typename T> class RandomT;
30 
31 /**
32  * Definition of the Random object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single or double precision float data type.
33  * @see RandomT
34  * @ingroup math
35  */
37 
38 /**
39  * Instantiation of the RandomT template class using a double precision float data type.
40  * @see RandomT
41  * @ingroup math
42  */
44 
45 /**
46  * Instantiation of the RandomT template class using a single precision float data type.
47  * @see RandomT
48  * @ingroup math
49  */
51 
52 /**
53  * This class provides several random functions for different data types using a floating point type for its elements that is specified by T.
54  * Beware: All function must not be used without a initialize() call for each thread!
55  * @tparam T Data type of passed and generated values
56  * @see Random, RandomF, RandomD.
57  * @ingroup math
58  */
59  template <typename T>
60 class RandomT : public RandomI
61 {
62  public:
63 
64  /**
65  * Returns a random sign (either +1 or -1).
66  * @return The random sign
67  */
68  static inline T sign();
69 
70  /**
71  * Returns a random sign (either +1 or -1).
72  * @param randomGenerator The random generator to be used
73  * @return The random sign
74  */
75  static inline T sign(RandomGenerator& randomGenerator);
76 
77  /**
78  * Returns a random number between two borders.
79  * @param lower The lower border, with range (-infinity, infinity)
80  * @param upper The upper border, with range [lower, infinity)
81  * @return Random number, with range [lower, upper]
82  */
83  static T scalar(const T lower, const T upper);
84 
85  /**
86  * Returns a random number between two borders using an explicit random generator.
87  * @param randomGenerator The random generator to be used
88  * @param lower The lower border, with range (-infinity, infinity)
89  * @param upper The upper border, with range [lower, infinity)
90  * @return Random number, with range [lower, upper]
91  */
92  static T scalar(RandomGenerator& randomGenerator, const T lower, const T upper);
93 
94  /**
95  * Returns a random number with Gaussian distribution.
96  * The returned value lies inside the range [-5 * sigma, 5 * sigma].
97  * @param sigma The sigma parameter defining the standard deviation of the Gaussian distribution, with range (0, infinity)
98  * @return Gaussian distributed random value
99  */
100  static T gaussianNoise(const T sigma);
101 
102  /**
103  * Returns a random number with Gaussian distribution using an explicit random generator.
104  * @param randomGenerator The random generator to be used
105  * The returned value lies inside the range [-5 * sigma, 5 * sigma].
106  * @param sigma The sigma parameter defining the standard deviation of the Gaussian distribution, with range (0, infinity)
107  * @return Gaussian distributed random value
108  */
109  static T gaussianNoise(RandomGenerator& randomGenerator, const T sigma);
110 
111  /**
112  * Returns a random 2D vector with Gaussian distribution.
113  * The returned value lies inside the range [-5 * sigma, 5 * sigma]x[-5 * sigma, 5 * sigma].
114  * @param sigmaX The sigma parameter defining the standard deviation of the Gaussian distribution for the x value, with range (0, infinity)
115  * @param sigmaY The sigma parameter defining the standard deviation of the Gaussian distribution for the y value, with range (0, infinity)
116  * @return Gaussian distributed random 2D vector
117  */
118  static VectorT2<T> gaussianNoiseVector2(const T sigmaX, const T sigmaY);
119 
120  /**
121  * Returns a random 2D vector with Gaussian distribution using an explicit random generator.
122  * The returned value lies inside the range [-5 * sigma, 5 * sigma]x[-5 * sigma, 5 * sigma].
123  * @param randomGenerator The random generator to be used
124  * @param sigmaX The sigma parameter defining the standard deviation of the Gaussian distribution for the x value, with range (0, infinity)
125  * @param sigmaY The sigma parameter defining the standard deviation of the Gaussian distribution for the y value, with range (0, infinity)
126  * @return Gaussian distributed random 2D vector
127  */
128  static VectorT2<T> gaussianNoiseVector2(RandomGenerator& randomGenerator, const T sigmaX, const T sigmaY);
129 
130  /**
131  * Returns a random 2D vector with length 1 which is equal distributed within a circle.
132  * @return The resulting random 2D vector, with vector2().length() == 1
133  */
134  static VectorT2<T> vector2();
135 
136  /**
137  * Returns a random 2D vector with length 1 which is equal distributed within a circle using an explicit random generator.
138  * @param randomGenerator The random generator to be used
139  * @return The resulting random 2D vector, with vector2().length() == 1
140  */
141  static VectorT2<T> vector2(RandomGenerator& randomGenerator);
142 
143  /**
144  * Returns a random 2D vector with coordinates in a given range.
145  * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
146  * @param max Maximum coordinate value for each axis, with range [min, infinity)
147  * @return Random 2D vector with random length
148  */
149  static VectorT2<T> vector2(const T min, const T max);
150 
151  /**
152  * Returns a random 2D vector with coordinates in a given range using an explicit random generator.
153  * @param randomGenerator The random generator to be used
154  * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
155  * @param max Maximum coordinate value for each axis, with range [min, infinity)
156  * @return Random 2D vector with random length
157  */
158  static VectorT2<T> vector2(RandomGenerator& randomGenerator, const T min, const T max);
159 
160  /**
161  * Returns a random 2D vector with coordinates in a given range.
162  * @param xMin Minimum x coordinate value for each axis, with range (-infinity, infinity)
163  * @param xMax Maximum x coordinate value for each axis, with range [xMin, infinity)
164  * @param yMin Minimum x coordinate value for each axis, with range (-infinity, infinity)
165  * @param yMax Maximum x coordinate value for each axis, with range [yMin, infinity)
166  * @return Random 2D vector with random length
167  */
168  static VectorT2<T> vector2(const T xMin, const T xMax, const T yMin, const T yMax);
169 
170  /**
171  * Returns a random 2D vector with coordinates in a given range using an explicit random generator.
172  * @param randomGenerator The random generator to be used
173  * @param xMin Minimum x coordinate value for each axis, with range (-infinity, infinity)
174  * @param xMax Maximum x coordinate value for each axis, with range [xMin, infinity)
175  * @param yMin Minimum x coordinate value for each axis, with range (-infinity, infinity)
176  * @param yMax Maximum x coordinate value for each axis, with range [yMin, infinity)
177  * @return Random 2D vector with random length
178  */
179  static VectorT2<T> vector2(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax);
180 
181  /**
182  * Returns a random 3D vector with length 1 which is equal distributed within a sphere.
183  * @return The resulting random 3D vector, with vector3().length() == 1
184  */
185  static VectorT3<T> vector3();
186 
187  /**
188  * Returns a random 3D vector with length 1 which is equal distributed within a sphere using an explicit random generator.
189  * @param randomGenerator The random generator to be used
190  * @return The resulting random 3D vector, with vector3().length() == 1
191  */
192  static VectorT3<T> vector3(RandomGenerator& randomGenerator);
193 
194  /**
195  * Returns a random 3D vector with coordinates in a given range.
196  * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
197  * @param max Maximum coordinate value for each axis, with range [min, infinity)
198  * @return Random 3D vector with random length
199  */
200  static VectorT3<T> vector3(const T min, const T max);
201 
202  /**
203  * Returns a random 3D vector with coordinates in a given range.
204  * @param range 3D vector defining the +/- ranges separately for each axis, with range [0, infinity] x [0, infinity] x [0, infinity]
205  * @return Random 3D vector with random length
206  */
207  static VectorT3<T> vector3(const VectorT3<T>& range);
208 
209  /**
210  * Returns a random 3D vector with coordinates in a given range using an explicit random generator.
211  * @param randomGenerator The random generator to be used
212  * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
213  * @param max Maximum coordinate value for each axis, with range [min, infinity)
214  * @return Random 3D vector with random length
215  */
216  static VectorT3<T> vector3(RandomGenerator& randomGenerator, const T min, const T max);
217 
218  /**
219  * Returns a random 3D vector with coordinates in a given range using an explicit random generator.
220  * @param randomGenerator The random generator to be used
221  * @param range 3D vector defining the +/- ranges separately for each axis, with range [0, infinity] x [0, infinity] x [0, infinity]
222  * @return Random 3D vector with random length
223  */
224  static VectorT3<T> vector3(RandomGenerator& randomGenerator, const VectorT3<T>& range);
225 
226  /**
227  * Returns a random 3D vector with coordinates in a given range using an explicit random generator.
228  * @param randomGenerator The random generator to be used
229  * @param xMin Minimum coordinate value for x-axis, with range (-infinity, infinity)
230  * @param xMax Maximum coordinate value for x-axis, with range [xMin, infinity)
231  * @param yMin Minimum coordinate value for y-axis, with range (-infinity, infinity)
232  * @param yMax Maximum coordinate value for y-axis, with range [yMin, infinity)
233  * @param zMin Minimum coordinate value for z-axis, with range (-infinity, infinity)
234  * @param zMax Maximum coordinate value for z-axis, with range [zMin, infinity)
235  * @return Random 3D vector with random length
236  */
237  static VectorT3<T> vector3(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax, const T zMin, const T zMax);
238 
239  /**
240  * Returns a random 4D vector with length 1 which is equal distributed within a hyper sphere.
241  * @return The resulting random 4D vector, with vector4().length() == 1
242  */
243  static VectorT4<T> vector4();
244 
245  /**
246  * Returns a random 4D vector with length 1 which is equal distributed within a hyper sphere using an explicit random generator.
247  * @param randomGenerator The random generator to be used
248  * @return The resulting random 4D vector, with vector4().length() == 1
249  */
250  static VectorT4<T> vector4(RandomGenerator& randomGenerator);
251 
252  /**
253  * Returns a random 4D vector with coordinates in a given range.
254  * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
255  * @param max Maximum coordinate value for each axis, with range [min, infinity)
256  * @return Random 4D vector with random length
257  */
258  static VectorT4<T> vector4(const T min, const T max);
259 
260  /**
261  * Returns a random 4D vector with coordinates in a given range using an explicit random generator.
262  * @param randomGenerator The random generator to be used
263  * @param min Minimum coordinate value for each axis, with range (-infinity, infinity)
264  * @param max Maximum coordinate value for each axis, with range [min, infinity)
265  * @return Random 4D vector with random length
266  */
267  static VectorT4<T> vector4(RandomGenerator& randomGenerator, const T min, const T max);
268 
269  /**
270  * Returns a random rotation as unit quaternion.
271  * @return Random rotation
272  */
273  static QuaternionT<T> quaternion();
274 
275  /**
276  * Returns a random rotation as unit quaternion using an explicit random generator.
277  * @param randomGenerator The random generator to be used
278  * @return Random rotation
279  */
280  static QuaternionT<T> quaternion(RandomGenerator& randomGenerator);
281 
282  /**
283  * Returns a random rotation.
284  * @return Random rotation
285  */
286  static RotationT<T> rotation();
287 
288  /**
289  * Returns a random rotation using an explicit random generator.
290  * @param randomGenerator The random generator to be used
291  * @return Random rotation
292  */
293  static RotationT<T> rotation(RandomGenerator& randomGenerator);
294 
295  /**
296  * Returns a random euler.
297  * @return Random euler
298  */
299  static EulerT<T> euler();
300 
301  /**
302  * Returns a random euler with angles in a given range.
303  * @param range Scalar defining the +/- range for each angle axis in radian, with range [0, PI/2)
304  * @return Random euler
305  */
306  static EulerT<T> euler(const T range);
307 
308  /**
309  * Returns a random euler with angles in a given range.
310  * This function allows to specified an angle range so that a minimal and maximal rotation is guaranteed.<br>
311  * First, three individual random angles are determined lying inside the specified range.<br>
312  * Second, the signs of the three angles are determined randomly (as the range is specified with positive values).<br>
313  * @param minRange Scalar defining the minimal range for each angle axis (in negative and positive direction) in radian, with range [0, PI/2)
314  * @param maxRange Scalar defining the minimal range for each angle axis (in negative and positive direction) in radian, with range [minRange, PI/2)
315  * @return Random euler
316  */
317  static EulerT<T> euler(const T minRange, const T maxRange);
318 
319  /**
320  * Returns a random euler using an explicit random generator using an explicit random generator.
321  * @param randomGenerator The random generator to be used
322  * @return Random euler
323  */
324  static EulerT<T> euler(RandomGenerator& randomGenerator);
325 
326  /**
327  * Returns a random euler with angles in a given range using an explicit random generator using an explicit random generator.
328  * @param randomGenerator The random generator to be used
329  * @param range Scalar defining the +/- range for each angle axis in radian, with range [0, PI/2)
330  * @return Random euler
331  */
332  static EulerT<T> euler(RandomGenerator& randomGenerator, const T range);
333 
334  /**
335  * Returns a random euler with angles in a given range using an explicit random generator using an explicit random generator.
336  * This function allows to specified an angle range so that a minimal and maximal rotation is guaranteed.<br>
337  * First, three individual random angles are determined lying inside the specified range.<br>
338  * Second, the signs of the three angles are determined randomly (as the range is specified with positive values).<br>
339  * @param randomGenerator The random generator to be used
340  * @param minRange Scalar defining the minimal range for each angle axis in radian, with range [0, PI/2)
341  * @param maxRange Scalar defining the minimal range for each angle axis in radian, with range [minRange, PI/2)
342  * @return Random euler
343  */
344  static EulerT<T> euler(RandomGenerator& randomGenerator, const T minRange, const T maxRange);
345 
346  protected:
347 
348  /**
349  * Returns the inverse of MAX_RAND.
350  * @return 1 / MAX_RAND
351  */
352  static constexpr T inverseMaxRand();
353 
354  /**
355  * Returns the inverse of RandomGenerator::randMax().
356  * @return 1 / RandomGenerator::randMax()
357  */
358  static constexpr T inverseMaxRandomGenerator();
359 };
360 
361 template <typename T>
363 {
364  if (rand() % 2u == 0u)
365  {
366  return T(-1);
367  }
368  else
369  {
370  return T(1);
371  }
372 }
373 
374 template <typename T>
375 inline T RandomT<T>::sign(RandomGenerator& randomGenerator)
376 {
377  if (randomGenerator.rand() % 2u == 0u)
378  {
379  return T(-1);
380  }
381  else
382  {
383  return T(1);
384  }
385 }
386 
387 template <typename T>
388 T RandomT<T>::scalar(const T lower, const T upper)
389 {
390  ocean_assert(std::is_floating_point<T>::value);
391 
392  ocean_assert(upper >= lower);
393 
394  return T(rand()) * inverseMaxRand() * (upper - lower) + lower;
395 }
396 
397 template <typename T>
398 T RandomT<T>::scalar(RandomGenerator& randomGenerator, const T lower, const T upper)
399 {
400  ocean_assert(std::is_floating_point<T>::value);
401 
402  ocean_assert(upper >= lower);
403 
404  return T(randomGenerator.rand()) * inverseMaxRandomGenerator() * (upper - lower) + lower;
405 }
406 
407 template <typename T>
409 {
410  const T maxValue = NumericT<T>::gaussianDistribution(T(0.0), sigma);
411 
412 #ifdef OCEAN_DEBUG
413  unsigned int debugN = 0u;
414 #endif
415 
416  while (true)
417  {
418  const T randomValue = scalar(T(-5.0) * sigma, T(5.0) * sigma);
419  const T check = scalar(T(0.0), maxValue);
420 
421  if (check <= NumericT<T>::gaussianDistribution(randomValue, sigma))
422  {
423  return randomValue;
424  }
425 
426  ocean_assert(++debugN < 1000);
427  }
428 }
429 
430 template <typename T>
431 T RandomT<T>::gaussianNoise(RandomGenerator& randomGenerator, const T sigma)
432 {
433  const T maxValue = NumericT<T>::gaussianDistribution(T(0.0), sigma);
434 
435 #ifdef OCEAN_DEBUG
436  unsigned int debugN = 0u;
437 #endif
438 
439  while (true)
440  {
441  const T randomValue = scalar(randomGenerator, T(-5.0) * sigma, T(5.0) * sigma);
442  const T check = scalar(randomGenerator, T(0.0), maxValue);
443 
444  if (check <= NumericT<T>::gaussianDistribution(randomValue, sigma))
445  {
446  return randomValue;
447  }
448 
449  ocean_assert(++debugN < 1000);
450  }
451 }
452 
453 template <typename T>
454 VectorT2<T> RandomT<T>::gaussianNoiseVector2(const T sigmaX, const T sigmaY)
455 {
456  const T x = gaussianNoise(sigmaX);
457  const T y = gaussianNoise(sigmaY);
458 
459  return VectorT2<T>(x, y);
460 }
461 
462 template <typename T>
463 VectorT2<T> RandomT<T>::gaussianNoiseVector2(RandomGenerator& randomGenerator, const T sigmaX, const T sigmaY)
464 {
465  const T x = gaussianNoise(randomGenerator, sigmaX);
466  const T y = gaussianNoise(randomGenerator, sigmaY);
467 
468  return VectorT2<T>(x, y);
469 }
470 
471 template <typename T>
473 {
474  while (true)
475  {
476  VectorT2<T> vector(scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)));
477 
478  if (vector.sqr() <= T(1.0) && vector.normalize())
479  {
480  return vector;
481  }
482  }
483 
484  ocean_assert(false && "This should never happen!");
485  return VectorT2<T>(1, 0);
486 }
487 
488 template <typename T>
490 {
491  while (true)
492  {
493  const T x = scalar(randomGenerator, T(-1.0), T(1.0));
494  const T y = scalar(randomGenerator, T(-1.0), T(1.0));
495 
496  VectorT2<T> vector(x, y);
497 
498  if (vector.sqr() <= T(1.0) && vector.normalize())
499  {
500  return vector;
501  }
502  }
503 
504  ocean_assert(false && "This should never happen!");
505  return VectorT2<T>(1, 0);
506 }
507 
508 template <typename T>
509 VectorT2<T> RandomT<T>::vector2(const T min, const T max)
510 {
511  return VectorT2<T>(scalar(min, max), scalar(min, max));
512 }
513 
514 template <typename T>
515 VectorT2<T> RandomT<T>::vector2(RandomGenerator& randomGenerator, const T min, const T max)
516 {
517  const T x = scalar(randomGenerator, min, max);
518  const T y = scalar(randomGenerator, min, max);
519 
520  return VectorT2<T>(x, y);
521 }
522 
523 template <typename T>
524 VectorT2<T> RandomT<T>::vector2(const T xMin, const T xMax, const T yMin, const T yMax)
525 {
526  return VectorT2<T>(scalar(xMin, xMax), scalar(yMin, yMax));
527 }
528 
529 template <typename T>
530 VectorT2<T> RandomT<T>::vector2(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax)
531 {
532  const T x = scalar(randomGenerator, xMin, xMax);
533  const T y = scalar(randomGenerator, yMin, yMax);
534 
535  return VectorT2<T>(x, y);
536 }
537 
538 template <typename T>
540 {
541  ocean_assert(range.x() >= T(0.0) && range.y() >= T(0.0) && range.z() >= T(0.0));
542 
543  return VectorT3<T>(scalar(-range.x(), range.x()), scalar(-range.y(), range.y()), scalar(-range.z(), range.z()));
544 }
545 
546 template <typename T>
548 {
549  ocean_assert(range.x() >= T(0.0) && range.y() >= T(0.0) && range.z() >= T(0.0));
550 
551  const T x = scalar(randomGenerator, -range.x(), range.x());
552  const T y = scalar(randomGenerator, -range.y(), range.y());
553  const T z = scalar(randomGenerator, -range.z(), range.z());
554 
555  return VectorT3<T>(x, y, z);
556 }
557 
558 template <typename T>
560 {
561  while (true)
562  {
563  VectorT3<T> vector(scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)));
564 
565  if (vector.sqr() <= T(1.0) && vector.normalize())
566  {
567  return vector;
568  }
569  }
570 
571  ocean_assert(false && "This should never happen!");
572  return VectorT3<T>(1, 0, 0);
573 }
574 
575 template <typename T>
577 {
578  while (true)
579  {
580  const T x = scalar(randomGenerator, T(-1.0), T(1.0));
581  const T y = scalar(randomGenerator, T(-1.0), T(1.0));
582  const T z = scalar(randomGenerator, T(-1.0), T(1.0));
583 
584  VectorT3<T> vector(x, y, z);
585 
586  if (vector.sqr() <= T(1.0) && vector.normalize())
587  {
588  return vector;
589  }
590  }
591 
592  ocean_assert(false && "This should never happen!");
593  return VectorT3<T>(1, 0, 0);
594 }
595 
596 template <typename T>
597 VectorT3<T> RandomT<T>::vector3(const T min, const T max)
598 {
599  return VectorT3<T>(scalar(min, max), scalar(min, max), scalar(min, max));
600 }
601 
602 template <typename T>
603 VectorT3<T> RandomT<T>::vector3(RandomGenerator& randomGenerator, const T min, const T max)
604 {
605  const T x = scalar(randomGenerator, min, max);
606  const T y = scalar(randomGenerator, min, max);
607  const T z = scalar(randomGenerator, min, max);
608 
609  return VectorT3<T>(x, y, z);
610 }
611 
612 template <typename T>
613 VectorT3<T> RandomT<T>::vector3(RandomGenerator& randomGenerator, const T xMin, const T xMax, const T yMin, const T yMax, const T zMin, const T zMax)
614 {
615  const T x = scalar(randomGenerator, xMin, xMax);
616  const T y = scalar(randomGenerator, yMin, yMax);
617  const T z = scalar(randomGenerator, zMin, zMax);
618 
619  return VectorT3<T>(x, y, z);
620 }
621 
622 template <typename T>
624 {
625  while (true)
626  {
627  VectorT4<T> vector(scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)), scalar(T(-1.0), T(1.0)));
628 
629  if (vector.sqr() <= T(1.0) && vector.normalize())
630  {
631  return vector;
632  }
633  }
634 
635  ocean_assert(false && "This should never happen!");
636  return VectorT4<T>(1, 0, 0, 0);
637 }
638 
639 template <typename T>
641 {
642  while (true)
643  {
644  const T x = scalar(randomGenerator, T(-1.0), T(1.0));
645  const T y = scalar(randomGenerator, T(-1.0), T(1.0));
646  const T z = scalar(randomGenerator, T(-1.0), T(1.0));
647  const T w = scalar(randomGenerator, T(-1.0), T(1.0));
648 
649  VectorT4<T> vector(x, y, z, w);
650 
651  if (vector.sqr() <= T(1.0) && vector.normalize())
652  {
653  return vector;
654  }
655  }
656 
657  ocean_assert(false && "This should never happen!");
658  return VectorT4<T>(1, 0, 0, 0);
659 }
660 
661 template <typename T>
662 VectorT4<T> RandomT<T>::vector4(const T min, const T max)
663 {
664  return VectorT4<T>(scalar(min, max), scalar(min, max), scalar(min, max), scalar(min, max));
665 }
666 
667 template <typename T>
668 VectorT4<T> RandomT<T>::vector4(RandomGenerator& randomGenerator, const T min, const T max)
669 {
670  const T x = scalar(randomGenerator, min, max);
671  const T y = scalar(randomGenerator, min, max);
672  const T z = scalar(randomGenerator, min, max);
673  const T w = scalar(randomGenerator, min, max);
674 
675  return VectorT4<T>(x, y, z, w);
676 }
677 
678 template <typename T>
680 {
681  return QuaternionT<T>(vector3(), scalar(T(0.0), NumericT<T>::pi2() - NumericT<T>::eps()));
682 }
683 
684 template <typename T>
686 {
687  const VectorT3<T> axis = vector3(randomGenerator);
688  const T angle = scalar(randomGenerator, T(0.0), NumericT<T>::pi2() - NumericT<T>::eps());
689 
690  return QuaternionT<T>(axis, angle);
691 }
692 
693 template <typename T>
695 {
696  return RotationT<T>(vector3(), scalar(T(0.0), NumericT<T>::pi2() - NumericT<T>::eps()));
697 }
698 
699 template <typename T>
701 {
702  const VectorT3<T> axis = vector3(randomGenerator);
703  const T angle = scalar(randomGenerator, T(0.0), NumericT<T>::pi2() - NumericT<T>::eps());
704 
705  return RotationT<T>(axis, angle);
706 }
707 
708 template <typename T>
710 {
712 }
713 
714 template <typename T>
716 {
717  ocean_assert(range >= T(0.0) && range < NumericT<T>::pi_2());
718 
719  return EulerT<T>(scalar(-range, range), scalar(-range, range), scalar(-range, range));
720 }
721 
722 template <typename T>
723 EulerT<T> RandomT<T>::euler(const T minRange, const T maxRange)
724 {
725  ocean_assert(minRange >= T(0.0) && minRange < NumericT<T>::pi_2());
726  ocean_assert(maxRange >= T(0.0) && maxRange < NumericT<T>::pi_2());
727  ocean_assert(minRange <= maxRange);
728 
729  return EulerT<T>(scalar(minRange, maxRange) * sign(), scalar(minRange, maxRange) * sign(), scalar(minRange, maxRange) * sign());
730 }
731 
732 template <typename T>
734 {
735  const T yaw = scalar(randomGenerator, -NumericT<T>::pi(), NumericT<T>::pi());
736  const T pitch = scalar(randomGenerator, -NumericT<T>::pi_2(), NumericT<T>::pi_2());
737  const T roll = scalar(randomGenerator, -NumericT<T>::pi(), NumericT<T>::pi());
738 
739  return EulerT<T>(yaw, pitch, roll);
740 }
741 
742 template <typename T>
743 EulerT<T> RandomT<T>::euler(RandomGenerator& randomGenerator, const T range)
744 {
745  ocean_assert(range >= T(0.0) && range < NumericT<T>::pi_2());
746 
747  const T yaw = scalar(randomGenerator, -range, range);
748  const T pitch = scalar(randomGenerator, -range, range);
749  const T roll = scalar(randomGenerator, -range, range);
750 
751  return EulerT<T>(yaw, pitch, roll);
752 }
753 
754 template <typename T>
755 EulerT<T> RandomT<T>::euler(RandomGenerator& randomGenerator, const T minRange, const T maxRange)
756 {
757  ocean_assert(minRange >= T(0.0) && minRange < NumericT<T>::pi_2());
758  ocean_assert(maxRange >= T(0.0) && maxRange < NumericT<T>::pi_2());
759  ocean_assert(minRange <= maxRange);
760 
761  const T yawSign = sign(randomGenerator);
762  const T yaw = scalar(randomGenerator, minRange, maxRange) * yawSign;
763 
764  const T pitchSign = sign(randomGenerator);
765  const T pitch = scalar(randomGenerator, minRange, maxRange) * pitchSign;
766 
767  const T rollSign = sign(randomGenerator);
768  const T roll = scalar(randomGenerator, minRange, maxRange) * rollSign;
769 
770  return EulerT<T>(yaw, pitch, roll);
771 }
772 
773 template <typename T>
775 {
776  return T(1.0) / T(RAND_MAX);
777 }
778 
779 template <typename T>
781 {
782  return T(1.0) / T(RandomGenerator::randMax());
783 }
784 
785 }
786 
787 #endif // META_OCEAN_MATH_RANDOM_H
This class implements an euler rotation with angles: yaw, pitch and roll.
Definition: Euler.h:80
This class provides basic numeric functionalities.
Definition: Numeric.h:57
static T gaussianDistribution(const T x, const T sigma)
Returns a value of the univariate Gaussian distribution centered around the origin.
Definition: Numeric.h:3433
This class implements a unit quaternion rotation.
Definition: Quaternion.h:100
This class implements a generator for random numbers.
Definition: RandomGenerator.h:42
unsigned int rand()
Returns the next random number.
Definition: RandomGenerator.h:161
static constexpr unsigned int randMax()
Returns the maximal random value of this generator.
Definition: RandomGenerator.h:183
This class provides base random functions and several random functions for integer data types.
Definition: RandomI.h:29
This class provides several random functions for different data types using a floating point type for...
Definition: Random.h:61
static T gaussianNoise(const T sigma)
Returns a random number with Gaussian distribution.
Definition: Random.h:408
static QuaternionT< T > quaternion()
Returns a random rotation as unit quaternion.
Definition: Random.h:679
static VectorT3< T > vector3()
Returns a random 3D vector with length 1 which is equal distributed within a sphere.
Definition: Random.h:559
static VectorT2< T > gaussianNoiseVector2(const T sigmaX, const T sigmaY)
Returns a random 2D vector with Gaussian distribution.
Definition: Random.h:454
static RotationT< T > rotation()
Returns a random rotation.
Definition: Random.h:694
static T sign()
Returns a random sign (either +1 or -1).
Definition: Random.h:362
static constexpr T inverseMaxRand()
Returns the inverse of MAX_RAND.
Definition: Random.h:774
static VectorT4< T > vector4()
Returns a random 4D vector with length 1 which is equal distributed within a hyper sphere.
Definition: Random.h:623
static VectorT2< T > vector2()
Returns a random 2D vector with length 1 which is equal distributed within a circle.
Definition: Random.h:472
static constexpr T inverseMaxRandomGenerator()
Returns the inverse of RandomGenerator::randMax().
Definition: Random.h:780
static EulerT< T > euler()
Returns a random euler.
Definition: Random.h:709
static T scalar(const T lower, const T upper)
Returns a random number between two borders.
Definition: Random.h:388
This class implements a axis-angle rotation using floating point values.
Definition: Rotation.h:79
This class implements a vector with two elements.
Definition: Vector2.h:96
T sqr() const
Returns the square of the vector length.
Definition: Vector2.h:621
bool normalize()
Normalizes this vector.
Definition: Vector2.h:600
This class implements a vector with three elements.
Definition: Vector3.h:97
const T & y() const noexcept
Returns the y value.
Definition: Vector3.h:812
bool normalize()
Normalizes this vector.
Definition: Vector3.h:647
const T & x() const noexcept
Returns the x value.
Definition: Vector3.h:800
const T & z() const noexcept
Returns the z value.
Definition: Vector3.h:824
T sqr() const
Returns the square of the vector length.
Definition: Vector3.h:670
This class implements a vector with four elements.
Definition: Vector4.h:97
T sqr() const
Returns the square of the vector length.
Definition: Vector4.h:616
bool normalize()
Normalizes this vector.
Definition: Vector4.h:593
RandomT< double > RandomD
Instantiation of the RandomT template class using a double precision float data type.
Definition: Random.h:43
RandomT< Scalar > Random
Definition of the Random object, depending on the OCEAN_MATH_USE_SINGLE_PRECISION either with single ...
Definition: Random.h:29
RandomT< float > RandomF
Instantiation of the RandomT template class using a single precision float data type.
Definition: Random.h:50
The namespace covering the entire Ocean framework.
Definition: Accessor.h:15