rand()
function interface, ThinAir provides this interface. However,
the rand()
interface is not very powerful or flexible. ThinAir also
provides an interface based on function objects.
5.1 The RandomGenerator Interface
The interface for all of the PRNG classes in the ThinAir library is defined by
the
The class also provides default implementation for some of the more general member functions. The members of this interface are
true
if the generator is unable to
function properly. Most algorithm-based generators will always return
false
. This member function is most useful for generators which
require some resource to run. These generators would return true
from bad()
if they are unable to acquire the needed resource.
IntSeed()
should have the same effect every time a
particular seed
value is used.
MaxRandom()
, inclusive.
double
number greater than or equal to 0.0 and
less than 1.0.
limit
-1, inclusive.
When performing simulations, it is often useful to rerun the same sequence of
random numbers through two different tests to compare responses. The
pToken
.
Additionally, the token classes also have a useful public interface:
CreateInitString()
and
reinitializes the token.
The
is
using Read()
.
os
using Print()
.
os
.
is
.
Each kind of PRNG has a token class derived from
5.2 The rand() Interface
Many programs written in C/C++ that need random number sequences use the
standard library rand()
function and its support functions. What you
may not have realized is that this function uses an implementation-defined
generator that may or may not be any good. So what do you do if your code
already uses rand()
, and you would like to try ThinAir?
This library contains a set of functions to duplicate the rand()
function interface wrapped around one of the classes from this library. The
idea is to provide the interface an average C programmer might find familiar,
with a simple method of changing the generator behind it.
By including the ThinAir rand_rng.h header file, you can have this
interface for any random number class you choose. To use one of these classes
with the rand()
interface, you must call the function setup_rand()
with a heap-based pointer to your chosen generator as the argument.
Like the standard library version of rand()
, the PRNG class library
provides the following interface, plus one extra function:
RAND_MAX
.
lim
.
IntSeed()
to the current
generator object with the argument seed
. This should change the
output sequence in some defined way.
srand()
function in a simple fashion to
change the current generator. Like the standard library equivalent,
randomize()
uses the current time as a seed
.
rand()
interface. This
function expects a pointer to a heap-based PRNG object.
5.3 The Function Object Interface
With support for the STL being written into the C++ standard (see
[5]), function objects are becoming an important idea to
consider. The STL supports a random_shuffle()
algorithm that accepts a
random number generator function object as an argument. In order to support
this algorithm, the PRNG class library provides a Function Object interface for
the classes. This interface also supplies other function objects which you may
find useful. This interface is provided by five classes, which are defined in
the file randfunc.h. All of these objects are used by calling the
operator()
member function.
Much of the time you want to use a PRNG the same way over and over again. These classes simplify this approach to using a PRNG.
Each of the Function Objects expects a pointer to a PRNG object that was
allocated from the heap. Don't pass the address of a stack-based PRNG object
to these functions. After construction, the Function Object owns the PRNG
object, so you do not need to delete
the PRNG.
5.3.1 RNGFuncAdaptor
This function object class defines an object which returns a random number from
a particular PRNG object. The operator()
member function calls the
Number()
member function of its PRNG. The constructor for this class has
the following prototype:
delete
s it when the object is
destroyed.
5.3.2 RNGFuncAdaptorFloat
This function object class defines an object which returns a random
double
greater than or equal to 0.0 and less than 1.0. The
operator()
member function calls the FloatNumber()
member
function of this PRNG. The constructor for this class has the following
prototype:
delete
s
it when the object is destroyed.
5.3.3 RNGFuncAdaptorLimit
This function object class defines an object which returns a number between 0
and a programmer-supplied limit value. The operator()
member function
calls the LimitedNumber()
member function of this PRNG passing the
selected limit. This class also has a second operator()
member function
that expects a single argument. This member function uses the supplied
argument instead of the stored limit when calling LimitedNumber()
. The
constructor for this class has the following prototype:
delete
s it
when the object is destroyed. The limit
argument defines the range of
values returned by the operator()
member function.
5.3.4 RNGFuncAdaptorPercentLikely
This function object class defines an object which returns a non-zero result
a programmer-supplied percentage of the time. The operator()
member
function returns a non-zero value percent
times out of a hundred on
average. This class also has a second operator()
member function that
expects a single argument. This member uses the supplied percent
instead
of the stored one. The constructor for this class has the following prototype:
delete
s it when the object is destroyed.
5.3.5 RNGFuncAdaptorRange
This function object class defines an object which returns a number between
two programmer-supplied limits. The operator()
member function returns a
value between lower
and upper
, inclusive. The constructor for this
class has the following prototype:
delete
s it
when the object is destroyed. The arguments lower
and upper
define the range of values that can be returned by the operator()
member function.