SPH
Classes | Functions
Rng.h File Reference

Random number generators. More...

#include "objects/containers/Array.h"
#include "objects/geometry/Box.h"
#include "objects/wrappers/AutoPtr.h"
#include <random>

Go to the source code of this file.

Classes

class  UniformRng
 Random number generator with uniform distribution. More...
 
class  BenzAsphaugRng
 Random number generator used in code SPH5 of Benz & Asphaug (1994). More...
 
class  HaltonQrng
 Quasi-random number generator. More...
 
class  IRng
 Polymorphic holder allowing to store any RNG (type erasure). More...
 
class  RngWrapper< TRng >
 

Functions

template<typename TRng , typename... TArgs>
AutoPtr< RngWrapper< TRng > > makeRng (TArgs &&... args)
 
template<typename TRng >
INLINE Float sampleNormalDistribution (TRng &rng, const Float mu, const Float sigma)
 Generates a random number from normal distribution, using Box-Muller algorithm. More...
 
template<typename TRng >
INLINE Float sampleExponentialDistribution (TRng &rng, const Float lambda)
 Generates a random number from exponential distribution. More...
 
template<typename TRng >
INLINE Size samplePoissonDistribution (TRng &rng, const Float lambda)
 Generates a random integer from Poisson distribution. More...
 
template<typename TRng >
INLINE Vector sampleUnitSphere (TRng &rng)
 Generates a random position on a unit sphere. More...
 
template<typename TRng , typename TFunc >
INLINE Float sampleDistribution (TRng &rng, const Interval &range, const Float upperBound, const TFunc &func)
 Generates a random number from a generic distribution, using rejection sampling. More...
 
template<typename TRng , typename TFunc >
INLINE Vector sampleDistribution (TRng &rng, const Box &box, const Float upperBound, const TFunc &func)
 Generates a random vector from a generic distribution, using rejection sampling. More...
 

Detailed Description

Random number generators.

Author
Pavel Sevecek (sevecek at sirrah.troja.mff.cuni.cz)
Date
2016-2021

Definition in file Rng.h.

Function Documentation

◆ makeRng()

template<typename TRng , typename... TArgs>
AutoPtr<RngWrapper<TRng> > makeRng ( TArgs &&...  args)

Definition at line 112 of file Rng.h.

◆ sampleDistribution() [1/2]

template<typename TRng , typename TFunc >
INLINE Vector sampleDistribution ( TRng &  rng,
const Box box,
const Float  upperBound,
const TFunc &  func 
)

Generates a random vector from a generic distribution, using rejection sampling.

Parameters
rngRandom number generator
boxExtents of generated vectors
upperBoundUpper bound for the values returned by the functor
funcProbability distribution function. Does not have to be normalized.

Definition at line 203 of file Rng.h.

◆ sampleDistribution() [2/2]

template<typename TRng , typename TFunc >
INLINE Float sampleDistribution ( TRng &  rng,
const Interval range,
const Float  upperBound,
const TFunc &  func 
)

Generates a random number from a generic distribution, using rejection sampling.

Note that this function may be very inefficient and should be used only if the distribution cannot be sampled with an explicit method.

Parameters
rngRandom number generator
rangeMinimal and maximal generated value
upperBoundUpper bound for the values returned by the functor
funcProbability distribution function. Does not have to be normalized.

Definition at line 184 of file Rng.h.

◆ sampleExponentialDistribution()

template<typename TRng >
INLINE Float sampleExponentialDistribution ( TRng &  rng,
const Float  lambda 
)

Generates a random number from exponential distribution.

Uses inverse transform sampling.

Definition at line 138 of file Rng.h.

◆ sampleNormalDistribution()

template<typename TRng >
INLINE Float sampleNormalDistribution ( TRng &  rng,
const Float  mu,
const Float  sigma 
)

Generates a random number from normal distribution, using Box-Muller algorithm.

Could be optimized (it discards the second independent value), but it's the algorith easiest to implement.

Definition at line 120 of file Rng.h.

◆ samplePoissonDistribution()

template<typename TRng >
INLINE Size samplePoissonDistribution ( TRng &  rng,
const Float  lambda 
)

Generates a random integer from Poisson distribution.

Uses the Knuth's algorithm.

Definition at line 152 of file Rng.h.

◆ sampleUnitSphere()

template<typename TRng >
INLINE Vector sampleUnitSphere ( TRng &  rng)

Generates a random position on a unit sphere.

Definition at line 166 of file Rng.h.