This reference page is linked to from the following overview
topics: Tips and
Tricks.
#include <random.h>
Class Description
- See also:
- Class Interface.
- Description:
- This class defines a Pseudo-random number generator that
precisely matches the behavior of the MSVCRT 6.0 random routines.
That is to say, for equivalent calls to ::srand() and Random::srand(),
both ::rand() and Random::rand()
will produce the same results.
The benefit, however, in having this class is that each
instantiation is independent, permitting several uncoupled random
number generators to be present in the system at once. Moreover,
each instantiation is automatically "pre-seeded", making calls to
Random::srand
unnecessary in most uses. Even arrays of Random items will operate
independently.
In addition to providing the analogues to the "stdlib" functions,
this class also provides two useful member functions which can be
used to get a random number bounded in either a float or int
interval.
Note: To use this class be sure to link to MAXUTIL.LIB.
- Sample Code:
-
#include "random.h"
...
Random r;
r.srand(1);
r.rand();
r.get();
r.get(16);
r.get(5,2);
r.get(1.0f);
r.get(1.0f,0.5f);
Random::s_rand_max;
...
Note in all "get" cases that contain limits they are specified
(max, min). Also be aware that the min value can be attained, but
the max cannot. That is to say min <= value < max.
- Data Members:
- static const int s_rand_max;
This is akin to the Windows API global RAND_MAX. The
constant RAND_MAX is the maximum value that can be returned
by the rand function. RAND_MAX is defined as the
value 0x7fff.
Public Member Functions
|
UtilExport |
Random
() |
UtilExport
void |
srand
(unsigned int seed=1) |
UtilExport
int |
rand
() |
int |
get (int
max_exclusive=s_rand_max+1,
int min_inclusive=0) |
float |
getf
(float max_exclusive=1.0f, float min_inclusive=0.0f) |
Static Public Attributes
|
static
UtilExport const int |
s_rand_max |
Constructor & Destructor Documentation
Member Function Documentation
UtilExport void srand |
( |
unsigned int |
seed = 1 |
) |
|
- Parameters:
- unsigned int seed = 1
To reinitialize the generator, use 1 as the seed argument. Any
other value for seed sets the generator to a random starting point.
rand retrieves the pseudorandom numbers that are generated.
Calling rand before any call to srand generates the
same sequence as calling srand with seed passed as 1.
int get |
( |
int |
max_exclusive = s_rand_max+1 , |
|
|
int |
min_inclusive =
0 |
|
) |
|
[inline] |
- Parameters:
- int max_exclusive = s_rand_max+1
The maximum value.
int min_inclusive = 0
The minimum value.
{
return (this->rand() % (max_exclusive - min_inclusive) + min_inclusive);
}
float getf |
( |
float |
max_exclusive =
1.0f , |
|
|
float |
min_inclusive =
0.0f |
|
) |
|
[inline] |
- Parameters:
- float max_exclusive = 1.0f
The maximum value.
float min_inclusive = 0.0f
The minimum value.
{
return (this->rand() / (s_rand_max+1.0f) * (max_exclusive - min_inclusive) + min_inclusive);
}
Member Data Documentation