Documentation>C API
Random number generator

Author
  Andrea Vedaldi

The module random.h implements random number generation in VLFeat. The generator is based on the popular Mersenne Twister algorithm [20] (which is the same as MATLAB random generator from MATLAB version 7.4 onwards).

# Getting started

In VLFeat, a random number generator is implemented by an object of type VlRand. The simplest way to obtain such an object is to get the default random generator by

VlRand * rand = vl_get_rand() ;
vl_int32 signedRandomInteger = vl_rand_int31(rand) ;
@code
Note that there is one such generator per thread (see
::vl_get_rand). If more control is desired, a new ::VlRand object can
be easily created. The object is lightweight, designed to be
allocated on the stack:
@code
VlRand rand ;
vl_rand_init (&rand) ;
vl_get_rand
VlRand * vl_get_rand(void)
Get the default random number generator.
Definition generic.c:1460
vl_int32
int vl_int32
Signed 32-bit integer.
Definition host.h:377
vl_rand_init
void vl_rand_init(VlRand *self)
Initialise random number generator.
Definition random.c:132
vl_rand_int31
vl_int32 vl_rand_int31(VlRand *self)
Generate a random INT31.
Definition random.h:106
VlRand
Random numbber generator state.
Definition random.h:21

The generator can be seeded by vl_rand_seed and vl_rand_seed_by_array. For instance:

vl_rand_seed (&rand, clock()) ;
vl_rand_seed
void vl_rand_seed(VlRand *self, vl_uint32 s)
Seed the state of the random number generator.
Definition random.c:144

The generator can be used to obtain random quantities of various types:

- vl_rand_int31, vl_rand_uint32 for 32-bit random integers;
- vl_rand_real1 for a double in [0,1];
- vl_rand_real2 for a double in [0,1);
- vl_rand_real3 for a double in (0,1);
- vl_rand_res53 for a double in [0,1) with high resolution.

There is no need to explicitly destroy a VlRand instance.