random.h
Functions | |
Returns true or false with 50% probability. | |
Returns true with the probability indicated by p . | |
Returns a random RGB color as an integer such as 0xff00ff . | |
Returns a random RGB color as a string such as "#ff00ff" . | |
randomElement(list) randomElement(set) randomElement(v) | Returns a randomly chosen element from the given collection. |
Returns a random integer in the range low to high , inclusive. | |
Returns a randomly chosen key from the given map. | |
Returns a random real number in the half-open interval [low .. high ). | |
Sets the internal random number seed to the specified value. |
bool randomBool();
true
or false
with 50% probability each.
This is equivalent to calling randomChance(0.5)
.
Usage:
if (randomBool()) ...
bool randomChance(double p);
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();
0xff00ff
.
Usage:
int color = randomColor();
Available since: 2016/08/02 version of C++ library
string randomColorString();
"#ff00ff"
.
Usage:
string color = randomColorString();
Available since: 2016/08/02 version of C++ library
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)
Usage:
element = randomElement(v);
Available since: 2015/04/15 version of C++ library
int randomInteger(int low, int high);
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)
Usage:
key = randomKey(map);
Available since: 2015/04/15 version of C++ library
double randomReal(double low, double high);
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);
Usage:
setRandomSeed(seed);