RandomSource

© 2011,2012 John Abbott
GNU Free Documentation License, Version 1.2



CoCoALib Documentation Index

User documentation for RandomSource

A RandomSource is a general source of (pseudo-)randomness: you can use it to produce random bits, random machine integers, and random big integers. For alternative ways of generating random values, see RandomBoolStream, RandomLongStream, and RandomZZStream; these specialized functions may be a little faster.

Examples

Constructors

The constructor for RandomSource has an optional single integer argument which is the initial seed -- it determines the initial state of the generator. If you do not give a seed, the default is 0.

    RandomSource  RndSrc;    // seeded with 0 by default
    RandomSource  RndSrc(n); // seeded with n

If you create more than one RandomSource object with the same seed, they will each produce exactly the same sequence of values. In particular, to obtain different results each time a program is run, you can, for instance, seed the generator with the system time (e.g. by supplying as argument time(0)); this is likely desirable unless you're trying to debug a randomized algorithm. See also the reseed function documented below.

Convenience at a price

For convenience, there is a global RandomSource object (living inside GlobalManager); you can get a reference to it by calling GlobalRandomSource(). It is very convenient, and is probably just what you want in a simple program, but using it will make your code thread-unsafe.

Operations

These are the convenient (thread-unsafe) functions for generating random values from GlobalRandomSource:

A cleaner way is to pass the random source as an argument: these are the functions for generating random values from a specified random source RndSrc:

A RandomSource object may be reseeded at any time; immediately after reseeding it will generate the same random sequence as a newly created RandomSource initialized with that same seed. The seed must be an integer value.

Note about thread-safety: the various operations on a fixed RandomSource object are not thread-safe; to achieve thread safety, you should use different objects in different threads. So, it is best not to use GlobalRandomSource() in a multi-threaded environment.

Maintainer documentation

Mostly quite straightforward since almost all the work is done by GMP.

RandomLong(RndSrc, lwb, upb) is a bit messy for two reasons:

  1. CoCoALib uses signed longs while GMP uses unsigned longs;
  2. the case when (lwb,upb) specify the whole range of representable longs requires special handling.

Bugs, shortcomings and other ideas

The printing function gives only partial information; e.g. two RandomSource objects with different internal states might be printed out identically.

The implementation simply calls the GMP pseudo-random generator; this generator is deterministic (so always produces the same sequence), but if you change versions of GMP, the sequence of generated values may change. You will have to read the GMP documentation to know more.

Main changes

2012