Visual Servoing Platform  version 3.6.1 under development (2024-11-21)

#include <visp3/core/vpUniRand.h>

Public Member Functions

 vpUniRand ()
 
 vpUniRand (uint64_t seed, uint64_t seq=0x123465789ULL)
 
double operator() ()
 
uint32_t next ()
 
int uniform (int a, int b)
 
float uniform (float a, float b)
 
double uniform (double a, double b)
 
void setSeed (uint64_t initstate, uint64_t initseq)
 

Static Public Member Functions

template<typename T >
static std::vector< T > shuffleVector (const std::vector< T > &inputVector, const int32_t &seed=-1)
 

Detailed Description

Class for generating random numbers with uniform probability density.

The algorithms and notations used are described in [41].

The following example also available in random.cpp shows how to use this class to generate 10 numbers between 0 and 5.

#include <iostream>
#include <visp3/core/vpConfig.h>
#include <visp3/core/vpUniRand.h>
int main()
{
#ifdef ENABLE_VISP_NAMESPACE
using namespace VISP_NAMESPACE_NAME;
#endif
vpUniRand rng;
for (int i = 0; i < 10; i++) {
std::cout << rng.uniform(0, 6) << std::endl; // produces int values
std::cout << rng.uniform(0.0, 6.0) << std::endl; // produces double values
}
std::vector<int> v;
for (unsigned int i = 0; i < 10; i++) {
v.push_back(i);
}
std::vector<int> shuffled_v = vpUniRand::shuffleVector<int>(v);
std::cout << "Original vector = [\t";
for (unsigned int i = 0; i < 10; i++) {
std::cout << v[i] << "\t";
}
std::cout << "]" << std::endl;
std::cout << "Shuffled vector = [\t";
for (unsigned int i = 0; i < 10; i++) {
std::cout << shuffled_v[i] << "\t";
}
std::cout << "]" << std::endl;
}
Class for generating random numbers with uniform probability density.
Definition: vpUniRand.h:127
int uniform(int a, int b)
Definition: vpUniRand.cpp:159

Once build, this previous code should produces an output similar to the following:

1
0.0582619
1
5.84875
1
3.86449
1
0.216396
5
5.41692
1
1.65448
0
3.31304
4
2.70563
0
4.86741
2
5.65826
Original vector = [ 0 1 2 3 4 5 6 7 8 9 ]
Shuffled vector = [ 2 4 7 8 5 1 3 6 9 0 ]
Examples
catchLuminanceMapping.cpp, catchParticleFilter.cpp, catchRand.cpp, catchRotation.cpp, photometricMappingVisualServoing.cpp, and tutorial-munkres-assignment.cpp.

Definition at line 126 of file vpUniRand.h.

Constructor & Destructor Documentation

◆ vpUniRand() [1/2]

BEGIN_VISP_NAMESPACE vpUniRand::vpUniRand ( )

Definition at line 72 of file vpUniRand.cpp.

◆ vpUniRand() [2/2]

vpUniRand::vpUniRand ( uint64_t  seed,
uint64_t  seq = 0x123465789ULL 
)

Create a pseudorandom number generator with uniform distribution.

Parameters
seed: Starting state for the RNG, you can pass any 64-bit value.
seq: Select the output sequence for the RNG, you can pass any 64-bit value, although only the low 63 bits are significant.
See also
setSeed

Definition at line 84 of file vpUniRand.cpp.

References setSeed().

Member Function Documentation

◆ next()

uint32_t vpUniRand::next ( void  )

Generates a pseudorandom uniformly distributed 32-bit unsigned integer (i.e., x where, 0 <= x < 2^32)

Examples
catchRand.cpp.

Definition at line 145 of file vpUniRand.cpp.

Referenced by vpMbtDistanceLine::buildFrom(), setSeed(), and uniform().

◆ operator()()

double vpUniRand::operator() ( )

Generates a pseudorandom uniformly distributed double number between [0, 1) range. This is equivalent to call uniform(0.0, 1.0);

Definition at line 94 of file vpUniRand.cpp.

References uniform().

◆ setSeed()

void vpUniRand::setSeed ( uint64_t  initstate,
uint64_t  initseq 
)

Initialize the random number generator.

Parameters
initstate: Starting state for the RNG, you can pass any 64-bit value.
initseq: Select the output sequence for the RNG, you can pass any 64-bit value, although only the low 63 bits are significant.
Note

For this generator, there are 2^63 possible sequences of pseudorandom numbers. Each sequence is entirely distinct and has a period of 2^64. The initseq argument selects which stream you will use. The initstate argument specifies where you are in that 264 period.

Calling setSeed with the same arguments produces the same output, allowing programs to use random number sequences repeatably.

If you want truly nondeterministic output for each run of your program, you should pass values that will be different from run to run. On a Unix system, /dev/random provides random bytes that can be used for initialization, but if you want a quick and dirty way to do the initialization, one option is to pass the current time and the address of the RNG itself.

Definition at line 202 of file vpUniRand.cpp.

References next().

Referenced by vpParticleFilter< MeasurementsType >::vpParticleFilter(), and vpUniRand().

◆ shuffleVector()

template<typename T >
static std::vector<T> vpUniRand::shuffleVector ( const std::vector< T > &  inputVector,
const int32_t &  seed = -1 
)
inlinestatic

Create a new vector that is a shuffled version of the inputVector.

Template Parameters
T: A class that possesses a copy constructor.
Parameters
inputVector: The input vector that must be shuffled. It will not be modified.
seed: The seed value.
Returns
std::vector<T> A vector containing the same objects than inputVector, but that are shuffled.
Examples
catchParticleFilter.cpp.

Definition at line 149 of file vpUniRand.h.

◆ uniform() [1/3]

double vpUniRand::uniform ( double  a,
double  b 
)

Generates a pseudorandom uniformly distributed double number between [a, b) range.

Parameters
a: lower inclusive boundary of the returned random number.
b: upper non-inclusive boundary of the returned random number.

Definition at line 179 of file vpUniRand.cpp.

References next().

◆ uniform() [2/3]

float vpUniRand::uniform ( float  a,
float  b 
)

Generates a pseudorandom uniformly distributed float number between [a, b) range.

Parameters
a: lower inclusive boundary of the returned random number.
b: upper non-inclusive boundary of the returned random number.

Definition at line 172 of file vpUniRand.cpp.

References next().

◆ uniform() [3/3]

int vpUniRand::uniform ( int  a,
int  b 
)

Generates a pseudorandom uniformly distributed int number between [a, b) range.

Parameters
a: lower inclusive boundary of the returned random number.
b: upper non-inclusive boundary of the returned random number.
Examples
catchLuminanceMapping.cpp, catchRand.cpp, and catchRotation.cpp.

Definition at line 159 of file vpUniRand.cpp.

Referenced by operator()(), and vpParticleFilter< MeasurementsType >::vpParticleFilter().