CGAL 6.1.2 - Geometric Object Generators: CGAL::Random Class Reference

CGAL 6.1.2 - Geometric Object Generators

Loading...
Searching...
No Matches
List of all members
CGAL::Random Class Reference
Reference Manual

`#include <CGAL/Random.h>`

## Definition

The class `Random` is a random numbers generator.

It generates uniformly distributed random `bool`, `int` and `double`. It can be used as the random number generating function object in the STL algorithm `std::random_shuffle()`.

Instances of `Random` can be seen as input streams. Different streams are independent of each other, i.e. the sequence of numbers from one stream does not depend upon how many numbers were extracted from the other streams. At each time, an instance has a state that uniquely determines the subsequent numbers being produced.

It can be very useful, e.g. for debugging, to reproduce a sequence of random numbers. This can be done by either initializing with a fixed seed, or by using the state functions as described below.

Note
  A `Random` object is not deterministic when used by several threads at the same time, even if a fixed seed has been chosen.

Implementation

We use the boost random library function `boost::rand48()` to generate the random numbers.

See also
  `CGAL::get_default_random()`

Examples
  Generator/random_polygon.cpp.

## Types
typedef unspecified_type | State
| The State type.

## Creation
| Random ()
| Default constructor.
| Random (unsigned int seed)
| Constructor initializing its internal state using `seed`.

## Operations
bool | get_bool ()
| returns a random `bool`.
template<int b>
int | get_bits ()
| returns a random `int` value from the interval \([0,2^b)\).
int | get_int (int lower, int upper)
| returns a random `int` from the interval `[lower,upper)`.
double | get_double (double lower=0.0, double upper=1.0)
| returns a random `double` from the interval `[lower,upper)`.

## Distributions
The following member functions are a 1-to-1 correspondence to some distributions from the boost random library.
template<typename IntType >
IntType | uniform_smallint (IntType lower=0, IntType upper=9)
| returns a random `IntType` from the interval `[lower,upper)`.
template<typename IntType >
IntType | uniform_int (IntType lower=0, IntType upper=9)
| returns a random `IntType` from the interval `[lower,upper)`.
template<typename RealType >
Realtype | uniform_real (RealType lower=0.0, RealType upper=1.0)
| returns a random `RealType` from the interval `[lower,upper)`.
template<typename RealType >
RealType | uniform_01 ()
| returns a random `RealType` from the interval `[0,1)`.
template<typename IntType >
IntType | operator() (IntType upper)
| returns `random` `uniform_int<IntType>( 0, upper-1)`.

## Seed and State Functions
unsigned int | get_seed () const
| returns the seed used for initialization.
void | save_state (State &state) const
| saves the current internal state in `state`.
void | restore_state (State const &state)
| restores the internal state from `state`.

## Equality Test
bool | operator== (Random const &random2) const
| returns `true`, iff the random object and `random2` have equal internal states.

## Constructor & Destructor Documentation

## ◆ Random() [1/2]

CGAL::Random::Random | ( |  | ) |

Default constructor.

The seed is chosen based on the system time.

## ◆ Random() [2/2]

CGAL::Random::Random | ( | unsigned int | seed | ) |

Constructor initializing its internal state using `seed`.

Equal values for `seed` result in equal sequences of random numbers.

## Member Function Documentation

## ◆ get_bits()

template<int b>

int CGAL::Random::get_bits | ( |  | ) |

returns a random `int` value from the interval \([0,2^b)\).

This is supposed to be efficient.

## ◆ uniform_01()

template<typename RealType >

RealType CGAL::Random::uniform_01 | ( |  | ) |

returns a random `RealType` from the interval `[0,1)`.

`RealType` can be `float`, `double`, etc.

## ◆ uniform_int()

template<typename IntType >

IntType CGAL::Random::uniform_int | ( | IntType | lower = `0`,
|  | IntType | upper = `9`
| ) |  |

returns a random `IntType` from the interval `[lower,upper)`.

`IntType` can be an integral type as `int`, `std::ptrdiff_t`, `std::size_t`,etc.

Warning
  In contrast to `get_int` this function may return `upper`.

## ◆ uniform_real()

template<typename RealType >

Realtype CGAL::Random::uniform_real | ( | RealType | lower = `0.0`,
|  | RealType | upper = `1.0`
| ) |  |

returns a random `RealType` from the interval `[lower,upper)`.

`RealType` can be `float`, `double`, etc.

## ◆ uniform_smallint()

template<typename IntType >

IntType CGAL::Random::uniform_smallint | ( | IntType | lower = `0`,
|  | IntType | upper = `9`
| ) |  |

returns a random `IntType` from the interval `[lower,upper)`.

`IntType` can be an integral type as `int`, `std::ptrdiff_t`, `std::size_t`,etc.

Warning
  In contrast to `get_int` this function may return `upper`.