[ThinAir Home]
[Table of Contents]
[The Class Interfaces]
[Concrete Classes]
6 Abstract Classes
Many generators are based on similar models. The abstract classes described in
this section formalize these similarities. If you are not interested in the
construction of new generators or in the architecture of the ThinAir library,
you might find Section 7, Concrete Classes more useful. In
the abstract classes, the functions Number()
and ReCreate()
are
always left undefined. None of these classes can be instantiated, they serve as
base classes for other parts of the library.
6.1 RandomGenerator
The RandomGenerator abstract base class is the root of the entire
hierarchy. This class interface is defined in Section 5.
6.2 CongruentialRandGen
Several types of PRNGs are based on the model of applying a simple function to
the last output and taking the modulus of the result.
Xn = f(Xn-1) mod m
This result is returned as the new output. The abstract class
CongruentialRandGen gathers the common features of these generators.
This class is derived directly from RandomGenerator and is
implemented in the files randcong.h and randcong.cpp. The
constructor for this class has the following prototype:
- explicit CongruentialRandGen( size_rand seed )
-
Stores the
seed
and then initializes itself.
In addition to the public interface, the CongruentialRandGen class
has a protected interface for use by its derived classes:
- size_rand Seed( void ) const;
-
Returns the initial seed value used when the current generator was
created.
- size_rand CurrSeed( void ) const;
-
Returns the current seed value that the generator uses in creating
the next random number.
- void SetSeed( size_rand seed );
-
Set the initial seed value used when the current generator was created.
- void SetCurrSeed( size_rand currSeed );
-
Set the current seed value that the generator uses to create the
next random number.
6.3 TableRandGen
The TableRandGen class defines an interface for generators that read
a number sequence from a table on disk. It is derived directly from
RandomGenerator and is implemented in the files randtabl.h
and randtabl.cpp. The TableRandGen class makes no assumptions
about the table contained in the file. The constructor for this class has the
following prototype:
- TableRandGen( const char *szFileName, streampos start )
-
Opens the file specified by
szFileName
and stores the value of
start
.
In addition to the public interface, the TableRandGen class has a
protected interface for use by its derived classes:
- ifstream &Table( void );
-
Returns the current file input stream used by this generator.
- const char *FileName( void ) const;
-
Returns the name of the file used by this generator.
- streampos Start( void ) const;
-
Returns the initial stream position specified when the current generator
was created.
- void SetStart( streampos start );
-
Sets the initial stream position specified when the current generator
was created.
- void MakeBad( void );
-
Marks the current generator as invalid. This member function is used by
derived classes to signal an error that prevents the generator from
running.
- void ClearBad( void );
-
Marks the current generator as valid. This member function is used by
derived classes to signal that the generator can continue to run.
6.4 TextTableRandGen
The TextTableRandGen class defines an interface for generators that
read a number sequence from a text table on disk. It is derived from
TableRandGen and is implemented in the files randtabl.h and
randtabl.cpp. The TextTableRandGen class makes only two
assumptions
about the file. The first is that it is basically a text file. The second is
that the file may have comments at the beginning of the file. The table will
begin after a line containing the phrase Begin Table. The constructor
for this class has the following prototype:
- TextTableRandGen( const char *szFileName, streampos start )
-
Calls the constructor for TableRandGen with the arguments
szFileName
and start
.
In addition to the public interface, the TextTableRandGen class has a
protected interface for use by its derived classes:
- void InitTable( void );
-
Finds the beginning of the table in the text file and moves the cursor
to an offset of
start
from the start of the table.
6.5 ByteTableRandGen
The ByteTableRandGen class specializes the TextTableRandGen
class to deal with a table of bytes, as opposed to a table containing a list of
long integers. The table is expected to be a list of digits that may be used
to construct the requested random number. The meaning of a given byte is not
specified in this abstract class. The ByteTableRandGen class is
implemented in the files randbtbl.h and randbtbl.cpp. The
constructor for this class has the following prototype:
- ByteTableRandGen( const char *szFileName, streampos start )
-
Calls the constructor for TextTableRandGen with the arguments
szFileName
and start
.
In addition to the public interface, the ByteTableRandGen class has a
protected interface for use by its derived classes:
- size_rand getNumber( unsigned digits );
-
Reads
digits
bytes from the table and constructs a number from
these bytes.
- unsigned getNumberDigits( size_rand limit );
-
Returns the number of digits needed to produce a number the size of
limit
.
6.6 CombineRandGen
One of the tradeoffs in building PRNGs is speed vs. unpredictability of the
sequence. One approach that has been tried to further randomize a fast
generator is to combine the output of two fast generators in some way. The
CombineRandGen abstract class defines a basic interface for a PRNG
that combines the output of two other PRNGs. The CombineRandGen class
is derived from RandomGenerator and is implemented in the files
randcomb.h and randcomb.cpp. The constructor for this class has
the following prototype:
- CombineRandGen( RandomGenerator *pGenX, RandomGenerator *pGenY )
-
Requires pointers to two heap-based PRNG objects. The
CombineRandGen object owns the two PRNGs and
delete
s
them when the object is destroyed.
In addition to the public interface, the CombineRandGen class has a
protected interface for use by its derived classes:
- RandomGenerator *pRandGenX( void ) const;
-
Returns a pointer to the first of two generators used by the current
generator.
- RandomGenerator *pRandGenY( void ) const;
-
Returns a pointer to the second of two generators used by the current
generator.
Once the CombineRandGen object has been created, it owns the PRNGs
passed to it. Do not try to delete
these pointers yourself.
6.7 AdaptorRandGen
The AdaptorRandGen abstract class defines a basic interface for a PRNG
that modifies the output of some other PRNG. It is derived from
RandomGenerator and is implemented in the files randadpt.h
and randadpt.cpp. Some possibilities include a generator that maps a
uniformly distributed random number sequence into a normal or Poisson
distribution. The constructor for this class has the following prototype:
- explicit AdaptorRandGen( RandomGenerator *pGen )
-
Requires a pointer to a heap-based PRNG object. The AdaptorRandGen
object owns this PRNG and
delete
s it when the object is destroyed.
In addition to the public interface, the AdaptorRandGen class has a
protected interface for use by its derived classes:
- RandomGenerator *pRandGen( void ) const;
-
Returns a pointer to the base generator modified by the current
generator.
Once the AdaptorRandGen object has been created, it owns the PRNG
passed to it. Do not try to delete
this pointer yourself.
6.8 SHARandGen
The SHARandGen class uses the SHA algorithm (see [9]) to
scramble the data in a buffer to generate a digested version of the buffer.
The SHARandGen class abstracts a generator that uses a constant string
and a variable source of numbers to fill the buffer that is to be digested. The
generator then uses the first sizeof(size_rand)
bytes of the digest as
the result of the generator. This class is derived from
RandomGenerator and is implemented in the files randsha.h and
randsha.cpp. The constructors for this class have the following
prototypes:
- SHARandGen( unsigned short size, const char *seedStr )
-
Requires a buffer of data that the object uses as part of the message in
the SHA algorithm. The length of the supplied buffer is
size
and
seedStr
points to the data. The constructor makes a copy of the data
for the object's use.
- explict SHARandGen( const char *seedStr )
-
Simplifies the construction of a SHARandGen object by allowing a
null-terminated string as its argument.
With many PRNGs, knowledge of the algorithm and a few consecutive numbers are
enough to duplicate the rest of the pseudo-random number sequence. The output
of the SHA-based generators should not be analyzable in this fashion.
For further information, contact G. Wade Johnson
(gwadej@anomaly.org).
© 1997 G. Wade Johnson.
All rights reserved.
http://www.anomaly.org/ThinAir/abstract.html