random.h

This file exports functions for generating pseudorandom numbers.
Functions
randomBool() Returns true or false with 50% probability.
randomChance(p) Returns true with the probability indicated by p.
randomColor() Returns a random RGB color as an integer such as 0xff00ff.
randomColorString() Returns a random RGB color as a string such as "#ff00ff".
randomElement(grid) 
randomElement(list) 
randomElement(set)  randomElement(v) 
Returns a randomly chosen element from the given collection.
randomInteger(lowhigh) Returns a random integer in the range low to high, inclusive.
randomKey(map) Returns a randomly chosen key from the given map.
randomReal(lowhigh) Returns a random real number in the half-open interval [low .. high).
setRandomSeed(seed) Sets the internal random number seed to the specified value.

Function detail


bool randomBool();
Returns true or false with 50% probability each. This is equivalent to calling randomChance(0.5).

Usage:

if (randomBool()) ...

bool randomChance(double p);
Returns true with the probability indicated by p. The argument p must be a floating-point number between 0 (never) and 1 (always). For example, calling randomChance(.30) returns true 30 percent of the time.

Usage:

if (randomChance(p)) ...

int randomColor();
Returns a random RGB color as an integer such as 0xff00ff.

Usage:

int color = randomColor();

string randomColorString();
Returns a random RGB color as a string such as "#ff00ff".

Usage:

string color = randomColorString();

template <typename T>
const T& randomElement(const Grid<T>& grid)

template <typename T>
const T& randomElement(const HashSet<T>& set)

template <typename T>
const T& randomElement(const LinkedList<T>& list)

template <typename T>
const T& randomElement(const Set<T>& set)

template <typename T>
const T& randomElement(const SparseGrid<T>& grid)

template <typename T>
const T& randomElement(const Vector<T>& v)
Returns a randomly chosen element of the given collection. Throws an error if the collection is empty.

Usage:

element = randomElement(v);

int randomInteger(int low, int high);
Returns a random integer in the range low to high, inclusive.

Usage:

int n = randomInteger(low, high);

template <typename K, typename V>
K randomKey(const HashMap<K, V>& map)

template <typename K, typename V>
K randomKey(const Map<K, V>& map)
Returns a randomly chosen key of the given map. Throws an error if the map is empty.

Usage:

key = randomKey(map);

double randomReal(double low, double high);
Returns a random real number in the half-open interval [low .. high). A half-open interval includes the first endpoint but not the second, which means that the result is always greater than or equal to low but strictly less than high.

Usage:

double d = randomReal(low, high);

void setRandomSeed(int seed);
Sets the internal random number seed to the specified value. You can use this function to set a specific starting point for the pseudorandom sequence or to ensure that program behavior is repeatable during the debugging phase.

Usage:

setRandomSeed(seed);