Class Random

3DS Max Plug-In SDK

Class Random

See Also: Class Interface.

class Random

Description:

This class is available in release 3.0 and later only.

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); // generally unnecessary, seeds generator like stdlib.h's srand()

r.rand(); // returns a random number a la stdlib.h's rand()

r.get(); // ditto

r.get(16); // returns 1 of 16 possible random numbers from 0 to 15 inclusive

r.get(5,2); // returns 1 of 3 possible random numbers from 2 to 4 inclusive

r.get(1.0f); // returns 1 of "Random::s_rand_max+1" floats, 0 <= value < 1

r.get(1.0f,0.5f); // as above, but limits the result to 0.5 <= value < 1

Random::s_rand_max; // similar to stdlib.h'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:

public:

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.

Methods:

public:

Prototype:

Random();

Remarks:

The constructor will automatically initialize the seed.

Prototype:

void srand(unsigned int seed = 1);

Remarks:

This method is akin to the global srand() function. From the Windows API documentation:

The srand function sets the starting point for generating a series of pseudorandom integers.

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.

Prototype:

int rand();

Remarks:

This method is akin to the global rand() function. From the Windows API documentation:

The rand function returns a pseudorandom integer in the range 0 to RAND_MAX. Use the srand function to seed the pseudorandom-number generator before calling rand.

Prototype:

inline int get(int max_exclusive = s_rand_max+1, int min_inclusive = 0);

Remarks:

Returns a random number in the half-open interval [min, max) such that r=get(max, min) := min <= r < max. Note that max is the first arg, and min is the second, permitting one to select, for example, an int in [0,5) = [0,4] with "get(5)". With no arguments, Random::get() is equivalent to Random::rand().

Parameters:

int max_exclusive = s_rand_max+1

The maximum value.

int min_inclusive = 0

The minimum value.

Prototype:

inline float getf(float max_exclusive = 1.0f, float min_inclusive = 0.0f);

Remarks:

Returns a random number in the half-open interval [min, max) such that r=get(max, min) := min <= r < max. Note that max is the first arg, and min is the second, permitting one to select, for example, a float in [0.0, 5.0) with "getf(5)". With no arguments, Random::getf() returns a float in [0.0, 1.0).

Parameters:

float max_exclusive = 1.0f

The maximum value.

float min_inclusive = 0.0f

The minimum value.