Utilities Package  Version Linux2023.0
Public Member Functions | List of all members
Scobey::RandomGenerator Class Reference

For generating pseudorandom integer, real, character and string values. More...

#include <utilities.h>

Public Member Functions

 RandomGenerator ()
 Default constructor, based on seed obtained from a call to the time() function. More...
 
 RandomGenerator (int userSeedValue)
 Constructor based on a client-supplied seed. More...
 
void reset ()
 Resets the random generator using a seed obtained by a call to the time() function. More...
 
void reset (int userSeedValue)
 Resets the random generator using a seed supplied by the client. More...
 
int getNext (int n)
 Gets a pseudorandom integer in the range [0,n). More...
 
int getNextInt (int n)
 Same as getNext(int n) above, but with a more explicit name, for readability rather than for ease of use when programming "generically". More...
 
int getNext (int low, int high)
 Gets a pseudorandom integer in the range [low,high]. More...
 
int getNextInt (int low, int high)
 Same as getNext(int low, int high) above, but with a more explicit name, for readability rather than for ease of use when programming "generically". More...
 
double getNext (double x)
 Gets a pseudorandom real (double) in the range [0,x). More...
 
double getNextDouble (double x)
 Same as getNext(double x) above, but with a more explicit name, for readability rather than for ease of use when programming "generically". More...
 
double getNext (double low, double high)
 Gets a pseudorandom real (double) value in the range [low,high). More...
 
double getNextDouble (double low, double high)
 Same as getNext(double low, double high) above, but with a more explicit name, for readability rather than for ease of use when programming "generically". More...
 
string getNext (const string &s)
 Gets a pseudorandom string based on the length and content of s. More...
 
string getNextString (const string &s)
 Same as getNext(const string& s) above, but with a more explicit name, for readability rather than for ease of use when programming "generically". More...
 
string getNext (const string &first, const string &second)
 Gets a random string in the "range" between the two strings first and second. More...
 
string getNextString (const string &first, const string &second)
 Same as getNext(const string& first, const string& second) above, but with a more explicit name, for readability rather than for ease of use when programming "generically". More...
 
string getNextStringFromCharRange (int size, char firstChar, char lastChar)
 Gets a pseudorandom string containing size characters from the character range [firstChar,secondChar]. More...
 
string getNextStringFromString (int size, const string &source)
 Gets a pseudorandom string containing size characters taken from the string s. More...
 

Detailed Description

For generating pseudorandom integer, real, character and string values.

The RandomGenerator class can be used to generate pseudorandom values of the following types:

Constructor & Destructor Documentation

◆ RandomGenerator() [1/2]

Scobey::RandomGenerator::RandomGenerator ( )

Default constructor, based on seed obtained from a call to the time() function.

If a random generator constructed by this constructor is used, the sequence of random values will be different each time.

Precondition
None
Postcondition
The generator has been initialized with a random seed value obtained by a call to the time() function.

◆ RandomGenerator() [2/2]

Scobey::RandomGenerator::RandomGenerator ( int  userSeedValue)

Constructor based on a client-supplied seed.

If a random generator constructed by this constructor is used, the userSeedValue used determines the sequence of random values obtained, and the same sequence of values can be obtained on a subsequent run by reusing the same userSeedValue. This same userSeedValue can be used to construct a new random generator object using this constructor, or to reset an already existing random generator object using the reset() method; either way, you get the same sequence of values as you did when that userSeedValue was employed previously.

Parameters
userSeedValueA seed value supplied by the client.
Precondition
userSeedValue has been initialized.
Postcondition
The generator has been initialized using a seed value (userSeedValue) supplied by the client.

Member Function Documentation

◆ reset() [1/2]

void Scobey::RandomGenerator::reset ( )

Resets the random generator using a seed obtained by a call to the time() function.

Returns
void
Precondition
None
Postcondition
The generator has been re-initialized using a random seed value obtained by a call to the time function.

◆ reset() [2/2]

void Scobey::RandomGenerator::reset ( int  userSeedValue)

Resets the random generator using a seed supplied by the client.

Returns
void
Precondition
userSeedValue has been initialized.
Postcondition
The generator has been re-initialized using a seed value (userSeedValue) supplied by the client.

◆ getNext() [1/6]

int Scobey::RandomGenerator::getNext ( int  n)

Gets a pseudorandom integer in the range [0,n).

Returns
A pseudorandom int value in the range [0,n).
Parameters
nA positive integer value.
Precondition
n has been initialized and n > 0.
Postcondition
The object's internal random number generator has been called once.

◆ getNextInt() [1/2]

int Scobey::RandomGenerator::getNextInt ( int  n)

Same as getNext(int n) above, but with a more explicit name, for readability rather than for ease of use when programming "generically".

◆ getNext() [2/6]

int Scobey::RandomGenerator::getNext ( int  low,
int  high 
)

Gets a pseudorandom integer in the range [low,high].

Returns
A pseudorandom integer value in the range [low,high].
Parameters
lowA positive integer value <= the value of high.
highA positive integer value >= the value of low.
Precondition
low and high have been initialized, and low <= high.
Postcondition
The object's internal random number generator has been called once.

◆ getNextInt() [2/2]

int Scobey::RandomGenerator::getNextInt ( int  low,
int  high 
)

Same as getNext(int low, int high) above, but with a more explicit name, for readability rather than for ease of use when programming "generically".

◆ getNext() [3/6]

double Scobey::RandomGenerator::getNext ( double  x)

Gets a pseudorandom real (double) in the range [0,x).

Returns
A pseudorandom double value in the range [0,x).
Parameters
xA positive real (double) number.
Precondition
x has been initialized and x > 0.
Postcondition
The object's internal random number generator has been called once.

◆ getNextDouble() [1/2]

double Scobey::RandomGenerator::getNextDouble ( double  x)

Same as getNext(double x) above, but with a more explicit name, for readability rather than for ease of use when programming "generically".

◆ getNext() [4/6]

double Scobey::RandomGenerator::getNext ( double  low,
double  high 
)

Gets a pseudorandom real (double) value in the range [low,high).

Returns
A pseudorandom real (double) value in the range [low,high).
Parameters
lowA positive real (double) value < the value of high.
highA positive real (double) value > the value of low.
Precondition
low and high have been initialized, and low < high.
Postcondition
The object's internal random number generator has been called once.

◆ getNextDouble() [2/2]

double Scobey::RandomGenerator::getNextDouble ( double  low,
double  high 
)

Same as getNext(double low, double high) above, but with a more explicit name, for readability rather than for ease of use when programming "generically".

◆ getNext() [5/6]

string Scobey::RandomGenerator::getNext ( const string &  s)

Gets a pseudorandom string based on the length and content of s.

Returns
A pseudorandom string object.
Parameters
sAny string of characters.
Precondition
s has been initialized and s.length() >= 2. Let a and b represent the first two distinct characters in s. Suppose a occurs in the first low contiguous locations in s. Suppose b occurs in the next high contiguous locations in s. It is required that a < b and that low <= high.
Postcondition
A pseudorandom string object has been returned, according to these rules:
  1. If low == high, then the length of any returned string is their common value.
  2. If low < high, then the length of any returned value is a random value in the range [low,high].
  3. If s.length() == low+high, the characters in the returned value are in the char range [a,b].
  4. If s.length() > low+high, the characters in any returned value come from the set of all characters in s, excluding the first two distinct characters.

◆ getNextString() [1/2]

string Scobey::RandomGenerator::getNextString ( const string &  s)

Same as getNext(const string& s) above, but with a more explicit name, for readability rather than for ease of use when programming "generically".

◆ getNext() [6/6]

string Scobey::RandomGenerator::getNext ( const string &  first,
const string &  second 
)

Gets a random string in the "range" between the two strings first and second.

Returns
A pseudorandom string object.
Parameters
firstThe "lower bound" of a "string range" from which the pseudorandom string object will be chosen.
secondThe "upper bound" of a "string range" from which the pseudorandom string object will be chosen.
Precondition
  1. first and second have been initialized.
  2. first <= second.
  3. first.length() <= second.length().
Postcondition
A pseudorandom string value r satisfying the following three conditions is returned:
  1. first <= r <= second.
  2. first.length() <= r.length() <= second.length().
  3. r contains only characters found in either first or second.

◆ getNextString() [2/2]

string Scobey::RandomGenerator::getNextString ( const string &  first,
const string &  second 
)

Same as getNext(const string& first, const string& second) above, but with a more explicit name, for readability rather than for ease of use when programming "generically".

◆ getNextStringFromCharRange()

string Scobey::RandomGenerator::getNextStringFromCharRange ( int  size,
char  firstChar,
char  lastChar 
)

Gets a pseudorandom string containing size characters from the character range [firstChar,secondChar].

Returns
A pseudorandom string object.
Parameters
sizeThe size of the string to be returned.
firstCharThe lower bound of the character range from which the characters for the returned string will be taken.
lastCharThe upper bound of the character range from which the characters for the returned string will be taken.
Precondition
size, first and last have been initialized, with size >=1, first <= last.
Postcondition
A pseudorandom string value containing size printable characters in the range [first,last] has been returned.

◆ getNextStringFromString()

string Scobey::RandomGenerator::getNextStringFromString ( int  size,
const string &  source 
)

Gets a pseudorandom string containing size characters taken from the string s.

Returns
A pseudorandom string object.
Parameters
sizeThe size of the string to be returned.
sourceThe string from which the characters for the returned string will be taken.
Precondition
size and source have been initialized, with size >= 1 and source containing only printable characters.
Postcondition
A random string value containing size printable characters, all of which appear in source, has been returned.

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