RandGen objects provide a source of computer-generated random numbers (sometimes known as pseudo-random numbers). By default, a RandGen object will produce a different series of numbers (through repeated calls to the RandInt and RandDouble methods) every time the program is run. When testing, though, it is often useful to have a program generate the same sequence of numbers each time it is run; this can be achieved by specifying a "seed" when the first RandGen object in the program is created.To construct random integers in a given range, client programs should use RandInt. To construct random doubles, client programs should use RandReal. The ranges for the return values for these functions are indicated with mathematical notation:
[0..max) means a number between 0 and max, not including max but including 0; [0..max] means a number between 0 and max, including both 0 and max.Acknowledgements: This class description is based on the RandGen class, which will be part of the upcoming AP Marine Biology Case Study. AP Case Studies are available from the College Board for face-to-face teaching purposes only.
(I'll provide a URL here when it is made public.)
class RandGen
{
public:
// Constructors
// The first RandGen object constructed in a program sets
// the "seed" for the series of random numbers that will be
// generated in the program. If the default constructor is used,
// a different series of numbers is produced every time the
// program is run. If the first RandGen object is constructed with
// a seed, the same series of numbers is produced every time.
RandGen(); // default constructor
RandGen(int seed); // used to produce the same series every
// time (most useful during testing)
// Observer Functions
// Ranges for return values are indicated with mathematical
// notation: [0..max) means a number between 0 and max, not
// including max but including 0; [0..max] means a number
// between 0 and max, including both 0 and max. For example,
// Call Returns value
// ---- -------------
// RandInt(5) between 0 and 4, might include 0 or 4
// RandInt(0, 5) between 0 and 5, might include 0 or 5
// RandReal() between 0 and 1, but not including 1
// RandReal(4, 6) between 4 and 6, might include 4 or 6
// All values in the indicated ranges are equally likely to be
// returned by RandInt and RandReal.
int RandInt(int max) const; // returns int in [0..max)
int RandInt(int low, int max) const; // returns int in [low..max]
double RandReal() const; // returns double in [0..1)
double RandReal(double low, double max) const; // returns double
// in [low..max]
private:
// not part of the public interface
};