#include <Random.h>
Public Member Functions | |
HaltonSequence () | |
HaltonSequence (uint32 dimensions, uint32 offset=0, uint32 *primes=nullptr) | |
~HaltonSequence () | |
void | Init (uint32 dimensions, uint32 offset=0, uint32 *primes=nullptr) |
void | Release () |
uint32 | GetNumDimensions () const |
uint32 | GetMemoryUsage () const |
uint32 | GetVectorNumber () const |
double | GetNextDimension () |
void | ResetNextDimension () |
void | Restart () |
void | Next () |
void | SetInstance (uint32 instance) |
void | operator++ () |
double | operator[] (uint32 j) const |
The Halton Sequence class. Halton sequences are pseudo random numbers, actually known as Quasi Random Numbers. They are often used in Monte Carlo sampling methods, because they are very good for that, especially for higher dimensions. The generated numbers are always the same. You can however add some real randomness to it to let the random numbers you get start at a given random offset (instance). The values generated try to keep an as large as possible distance between eachother, to prevent groups of points very close to eachother at some spots. This is one of the reasons why they are so cool. They work basically the same as stratisfied sampling your values, but with the nice feature that you do not have to know upfront how many random numbers you will need. So you can keep asking for the next value in the set. The values returned by the sequence are in range of [0..1]. If for example you want to generate random points in a 2D box, you can create a two dimensional HaltonSequence. Let me demonstrate with some example code:
// create a two dimensional Halton sequence HaltonSequence seq(2); // generate 100 random points inside the 1x1 unit 2D box. for (uint32 i=0; i<100; i++) { float x = seq[0]; float y = seq[1]; seq.Next(); }
You now might want to ask, why not just use the Random::RandF() function for this? Well, the answer is that the distribution of the samples is much more evenly with the Halton sequences as with just normal random numbers using Random::RandF(). When not using Random::RandF() you can get areas where there are more points as in other areas, so no even distribution. This can lead to variance in estimates using random values. HaltonSequences converge much faster, so result in a more accurate estimate.
MCore::HaltonSequence::HaltonSequence | ( | ) |
The default constructor. Be sure to call the Init(...) method before using any other methods of this class.
MCore::HaltonSequence::HaltonSequence | ( | uint32 | dimensions, |
uint32 | offset = 0 , |
||
uint32 * | primes = nullptr |
||
) |
Extended constructor. This constructor automatically calls the Init method. Please keep in mind that MCore needs to be initialized before doing this, so if you have a global HaltonSequence object that uses this constructor, this will cause a crash. In that case, use the Init method with the default constructor that takes no parameters.
dimensions | The number of dimensions for the sequence. |
offset | The offset to start in the sequence. |
primes | The list of prime values. Keep this nullptr when you want to generate them automatically. |
MCore::HaltonSequence::~HaltonSequence | ( | ) |
Destructor.
|
inline |
Get the memory usage in bytes by this sequence.
|
inline |
Get the value of current dimension, and go to the next dimension.
|
inline |
Returns the number of dimensions in the sequence.
|
inline |
Get the current vector number.
void MCore::HaltonSequence::Init | ( | uint32 | dimensions, |
uint32 | offset = 0 , |
||
uint32 * | primes = nullptr |
||
) |
Initialize the sequence. You can also call the Init method multiple times with different parameters to change its settings.
dimensions | The number of dimensions for the sequence. |
offset | The offset to start in the sequence. |
primes | The list of prime values. Keep this nullptr when you want to generate them automatically. |
|
inline |
Get the next values in the sequence. So update the dimension values.
void MCore::HaltonSequence::operator++ | ( | ) |
Get the next values in the sequence. So update the dimension values.
|
inline |
Get a value for a given dimension. (*sequence[0]) would be the value of the first dimension and (*sequence[1]) would be the value of the second dimension, etc.
void MCore::HaltonSequence::Release | ( | ) |
Release all allocated memory. This is automatically called by the destructor.
|
inline |
Reset the dimension stepping (by GetNextDimension()) and go to the first dimension again.
|
inline |
Restart the sequence.
void MCore::HaltonSequence::SetInstance | ( | uint32 | instance | ) |
Set the instance in the sequence. So the startpoint/offset in the sequence, where 0 would jump to the beginning again.
instance | The instance value, so the startpoint in the sequence. |