![]() |
ImFusion SDK 4.3
|
Convenient random number generation that offers automatic seeding and works with standard distributions (boost distributions are the default, but a distribution of choice can be used). More...
Convenient random number generation that offers automatic seeding and works with standard distributions (boost distributions are the default, but a distribution of choice can be used).
The ImFusion::Random namespace provides a generator class which uses the Pcg64Engine class, which is based on the PCG library. The engine is by default automatically seeded, but can also accept a seed when necessary (for example, in reproducible tests). A global generator (singleton) is offered: this generator is thread-safe and default-seeded. Access to the global generator is possible via the global function Random::globalGenerator
.
An instance of the generator class (or the global one) offers a number of convenient member-functions. getUniformInteger
and getUniformReal
generate uniformly-distributed integers and real numbers respectively. getNormal
generates normally-distributed random numbers.
If you need to use another distribution than the uniform one, call getVariate
and specify the return type and the distribution class to use.
Next are the pick
and choose
functions, which allow to pick a random element (uniformly) in a range.
You can also pick multiple elements at the same time using pickN
. Note that this is different from calling pick N times, since pickN will not return duplicates.
Next are the "fill..." functions, which mirror the getUniform...
and getVariate
functions but write to a range instead of returning a value. fillVariate
can be used by specifying a distribution class. The type of elements in the range should match the distribution's output type.
Next, shuffle
and partition
are just shorthands for std::shuffle and std::stable_partition. The former shuffles the elements in a range, the latter places a number of the range's elements at the beginning of the range.
All of the above functionality is provided also via a global instance of a Random::Generator
. This instance can be retrieved and used as follows:
Note that this global generator is thread safe. Although convenient, usage of global objects such as this generator shall be avoided in favor of local objects.
When you call the globalGenerator
function in the Random namespace, you use a default-constructed global generator. Default-constructed generators are automatically seeded using several different sources of entropy and mixing them together. See the documentation of the Pcg64Engine for more information.
You can also seed the generator yourself on construction. The seed type is Generator::Seed
(or uint64_t since the engine is a PCG 64-bit)
Generator::reseed
can also be used if you want to provide a new seed without instantiating another generator. If no argument is given, the seed is produced using the same sources of entropy described above. Note that instead of using the same seed multiple times for reproducible processes, you may want to keep a default-constructed main generator instance and copy-construct a new instance to be used in the reproducible process. Copy-construction and copy-assignment guarantee the same output for the copy and the original.
Namespaces | |
namespace | ImFusion |
Namespace of the ImFusion SDK. | |
namespace | ImFusion::Random |
Random number generation with convenience functions | |
Classes | |
class | Generator |
Convenience class for generating random numbers from various distributions. More... | |
class | Pcg64Engine |
Wrapper class for a 64-bit PCG engine. More... | |
Free functions | |
Generator & | globalGenerator () |
Returns a thread-safe default-seeded generator (singleton) | |
template<typename T> | |
T | nextReal (T n) |
Returns the next number representable by T. | |
template<typename T> | |
T | previousReal (T n) |
Returns the previous number representable by T. | |
T nextReal | ( | T | n | ) |
#include <ImFusion/Core/Random.h>
Returns the next number representable by T.
Used for example to transform a semi-open interval [a, b) into a closed one [a, b]. One use case could be to make sure the lower bound of getUniformReal cannot be returned (because it would cause a division by zero, for example).
T previousReal | ( | T | n | ) |
#include <ImFusion/Core/Random.h>
Returns the previous number representable by T.
Used for example to transform a closed interval [a, b] into a semi-open one [a, b). One use case could be to make sure the upper bound of getUniformReal cannot be returned (because it would cause a division by zero, for example).