Dv::Util::Random Class Reference

Class supporting random number generation using /dev/urandom Example usage. More...

#include <random.h>

List of all members.

Static Public Member Functions

static unsigned long rand () throw (std::runtime_error)
 Produces a random unsigned long taken from /dev/urandom.
static bool gamble (size_t chances, size_t out_of) throw (std::runtime_error)
 Gamble such that the chance of 'true' is N out of M.
static size_t pick_one (size_t min, size_t max) throw (std::runtime_error)
 Pick a number out of a range.

Detailed Description

Class supporting random number generation using /dev/urandom Example usage.

 unsigned long key = Dv::Util::Random::rand(); 

Definition at line 15 of file random.h.


Member Function Documentation

static unsigned long Dv::Util::Random::rand (  )  throw (std::runtime_error) [static]

Produces a random unsigned long taken from /dev/urandom.

An exception is thrown if anything goes wrong, e.g. the file /dev/urandom cannot be opened.

Should probably automatically switch to another implementation if /dev/urandom is not available. This is probably a naive approach but will do for the simple purposes it is used for.

Referenced by gamble().

static bool Dv::Util::Random::gamble ( size_t  chances,
size_t  out_of 
) throw (std::runtime_error) [inline, static]

Gamble such that the chance of 'true' is N out of M.

E.f. a gamble(15,100) will return true in 15% of the cases.

Parameters:
chances N in probablity N/M
out_of M in probablity N/M
Returns:
true with chances/out_of probability

Definition at line 35 of file random.h.

References rand().

static size_t Dv::Util::Random::pick_one ( size_t  min,
size_t  max 
) throw (std::runtime_error) [static]

Pick a number out of a range.

Parameters:
min of range [min..max[
max of range [min..max[
Returns:
'random' number out of [min..max[
Exceptions:
if min > max

The documentation for this class was generated from the following file:

dvutil-1.0.10 [ 5 December, 2009]