VLFeat - Documentation > C API

# VLFeat.org

Documentation> C API

- Main Page
- Related Pages
- Data Structures
- Files

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) ;

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

vl_rand_seed (&rand, clock()) ;

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.