digiKam
Digikam::RandomNumberGenerator Class Reference

Public Member Functions

quint32 currentSeed () const
 
double number (double min, double max)
 
int number (int min, int max)
 
 RandomNumberGenerator ()
 
void reseed ()
 
void seed (quint32 seed)
 
quint32 seedByTime ()
 
quint32 seedNonDeterministic ()
 
bool yesOrNo (double p)
 
 ~RandomNumberGenerator ()
 

Static Public Member Functions

static quint32 nonDeterministicSeed ()
 
static quint32 timeSeed ()
 

Detailed Description

This class differs from standard pseudo random number generators (rand()) in these points:

  • it uses a specified, independently implemented algorithm identical across platforms
  • provides access to the used seed
  • it can thus guarantee replayable sequences
  • it provides convenient seeding of varying quality

Constructor & Destructor Documentation

◆ RandomNumberGenerator()

Digikam::RandomNumberGenerator::RandomNumberGenerator ( )
explicit

Constructs a random number generator that is seeded with a constant value. It is recommended to call a seed method after construction.

◆ ~RandomNumberGenerator()

Digikam::RandomNumberGenerator::~RandomNumberGenerator ( )

Member Function Documentation

◆ currentSeed()

quint32 Digikam::RandomNumberGenerator::currentSeed ( ) const

Retrieves the current seed. Can be used for seed(quint32) to replay the results again.

◆ nonDeterministicSeed()

quint32 Digikam::RandomNumberGenerator::nonDeterministicSeed ( )
static

Produces a non-deterministic seed, as used by seedNonDeterministic()

References seed().

Referenced by seedNonDeterministic().

◆ number() [1/2]

double Digikam::RandomNumberGenerator::number ( double  min,
double  max 
)

Returns a random double in the interval [min, max) (including min, excluding max) Warning: this method is non re-entrant.

◆ number() [2/2]

int Digikam::RandomNumberGenerator::number ( int  min,
int  max 
)

Returns a random integer in the interval [min, max] (including min and max). Warning: this method is non re-entrant.

Referenced by Digikam::DImg::convertDepth().

◆ reseed()

void Digikam::RandomNumberGenerator::reseed ( )

Seeds the generator again with the currentSeed(). This is not a no-op, rather, the sequence of random numbers starts again from its beginning after each re-seed. Equivalent to seed(currentSeed())

References seed().

◆ seed()

void Digikam::RandomNumberGenerator::seed ( quint32  seed)

Seeds the generator with the given value. This is not meant to be called with a constant value, but with a value retrieved from currentSeed() on a previous run. Across platforms, the same sequence of random numbers will be generated for the same seed.

Referenced by nonDeterministicSeed(), reseed(), and timeSeed().

◆ seedByTime()

quint32 Digikam::RandomNumberGenerator::seedByTime ( )

Seeds the generator by current time. This is common practice and good enough for most purposes. Returns the new currentSeed().

References timeSeed().

◆ seedNonDeterministic()

quint32 Digikam::RandomNumberGenerator::seedNonDeterministic ( )

Seeds the generator from a non-deterministic random number generator. This is the most secure seeding method. Returns the new currentSeed().

References nonDeterministicSeed().

◆ timeSeed()

quint32 Digikam::RandomNumberGenerator::timeSeed ( )
static

Produces a seed that includes at least the time as source of random data

References seed().

Referenced by Digikam::DistortionFXFilter::DistortionFXFilter(), and seedByTime().

◆ yesOrNo()

bool Digikam::RandomNumberGenerator::yesOrNo ( double  p)

Returns true with a probability of p (where p shall be in the interval [0, 1]) Warning: this method is non re-entrant.


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