38 #ifndef FCL_MATH_RNG_INL_H 39 #define FCL_MATH_RNG_INL_H 41 #include "fcl/math/rng.h" 53 : generator_(detail::Seed::getNextSeed()), uniDist_(0, 1), normalDist_(0, 1)
61 return uniDist_(generator_);
68 assert(lower_bound <= upper_bound);
70 return (upper_bound - lower_bound) * uniDist_(generator_) + lower_bound;
77 int r = (int)floor(
uniformReal((S)lower_bound, (S)(upper_bound) + 1.0));
79 return (r > upper_bound) ? upper_bound : r;
86 return uniDist_(generator_) <= 0.5;
93 return normalDist_(generator_);
100 return normalDist_(generator_) * stddev + mean;
104 template <
typename S>
107 assert(r_min <= r_max);
109 const auto mean = r_max - r_min;
110 auto v =
gaussian(mean, mean / focus);
115 auto r = v >= 0.0 ? v + r_min : r_min;
117 return r > r_max ? r_max : r;
121 template <
typename S>
125 (S)r_min, (S)(r_max) + 1.0, focus));
127 return (r > r_max) ? r_max : r;
131 template <
typename S>
134 auto x0 = uniDist_(generator_);
135 auto r1 = std::sqrt(1.0 - x0), r2 = std::sqrt(x0);
138 auto c1 = std::cos(t1);
139 auto s1 = std::sin(t1);
140 auto c2 = std::cos(t2);
141 auto s2 = std::sin(t2);
149 template <
typename S>
153 value[1] = std::acos(1.0 - 2.0 * uniDist_(generator_)) -
constants<S>::pi() / 2.0;
158 template <
typename S>
163 auto r = std::sqrt(a * r_max * r_max + (1 - a) * r_min * r_min);
165 x = r * std::cos(theta);
166 y = r * std::sin(theta);
170 template <
typename S>
172 S r_min, S r_max, S& x, S& y, S& z)
177 auto r = std::pow(a*std::pow(r_max, 3) + (1 - a)*std::pow(r_min, 3), 1/3.0);
178 auto theta = std::acos(1 - 2 * b);
181 auto costheta = std::cos(theta);
182 auto sintheta = std::sin(theta);
183 auto cosphi = std::cos(phi);
184 auto sinphi = std::sin(phi);
186 y = r * sintheta * cosphi;
187 z = r * sintheta * sinphi;
191 template <
typename S>
194 if (detail::Seed::isFirstSeedGenerated())
196 std::cerr <<
"Random number generation already started. Changing seed now " 197 <<
"will not lead to deterministic sampling." << std::endl;
202 std::cerr <<
"Random generator seed cannot be 0. Using 1 instead." 204 detail::Seed::setUserSetSeed(1);
208 detail::Seed::setUserSetSeed(seed);
213 template <
typename S>
216 return detail::Seed::getFirstSeed();
void quaternion(S value[4])
Uniform random unit quaternion sampling. The computed value has the order (x,y,z,w) ...
Definition: rng-inl.h:132
Main namespace.
Definition: broadphase_bruteforce-inl.h:45
void ball(S r_min, S r_max, S &x, S &y, S &z)
Uniform random sample in a ball with radius from r_min to r_max.
Definition: rng-inl.h:171
S uniformReal(S lower_bound, S upper_bound)
Generate a random real within given bounds: [lower_bound, upper_bound)
Definition: rng-inl.h:66
RNG()
Constructor. Always sets a different random seed.
Definition: rng-inl.h:52
static std::uint_fast32_t getSeed()
Get the seed used for random number generation. Passing the returned value to setSeed() at a subseque...
Definition: rng-inl.h:214
void disk(S r_min, S r_max, S &x, S &y)
Uniform random sample on a disk with radius from r_min to r_max.
Definition: rng-inl.h:159
*void eulerRPY(S value[3])
Uniform random sampling of Euler roll-pitch-yaw angles, each in the range [-pi, pi). The computed value has the order (roll, pitch, yaw)
Definition: rng-inl.h:150
S halfNormalReal(S r_min, S r_max, S focus=3.0)
Generate a random real using a half-normal distribution. The value is within specified bounds [r_min...
Definition: rng-inl.h:105
S gaussian(S mean, S stddev)
Generate a random real using a normal distribution with given mean and variance.
Definition: rng-inl.h:98
static constexpr S pi()
The mathematical constant pi.
Definition: constants.h:49
int uniformInt(int lower_bound, int upper_bound)
Generate a random integer within given bounds: [lower_bound, upper_bound].
Definition: rng-inl.h:75
int halfNormalInt(int r_min, int r_max, S focus=3.0)
Generate a random integer using a half-normal distribution. The value is within specified bounds ([r_...
Definition: rng-inl.h:122
S uniform01()
Generate a random real between 0 and 1.
Definition: rng-inl.h:59
bool uniformBool()
Generate a random boolean.
Definition: rng-inl.h:84
static void setSeed(std::uint_fast32_t seed)
Set the seed for random number generation. Use this function to ensure the same sequence of random nu...
Definition: rng-inl.h:192
S gaussian01()
Generate a random real using a normal distribution with mean 0 and variance 1.
Definition: rng-inl.h:91