Documentation>C API
Random number generator

Table of Contents

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) ;
VlRand * vl_get_rand(void)
Get the default random number generator.
Definition: generic.c:1460
int vl_int32
Signed 32-bit integer.
Definition: host.h:377
void vl_rand_init(VlRand *self)
Initialise random number generator.
Definition: random.c:132
vl_int32 vl_rand_int31(VlRand *self)
Generate a random INT31.
Definition: random.h:106
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()) ;
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:

There is no need to explicitly destroy a VlRand instance.