Class RandGenerator

3DS Max Plug-In SDK

Class RandGenerator

class RandGenerator

Description:

This class is available in release 4.0 and later only.

This class has interfaces for srand() and rand() methods of VC++ and other functions for random number generation. The srand() and rand() methods from stdlib.h have two main problems:

a) It's not satisfactorily random. The rand() function returns a pseudorandom integer in the range 0 to 0x7fff=32767. If we need a lot of random numbers using rand() (i.e. for generating 100,000 particles), we run out of continuity of random numbers. Generated random numbers becomes too discrete.

b) The rand() method is global function, not class object. Hence it is shared between all modules of your plug-in. Changes in one module may change randomness pattern in other independent module. To solve this contradiction, rand methods have to be implemented as a class object.

The RandGenerator does exactly that. It has much more random numbers: RAND_MAX = 0xFFFFFFFF = 4,294,967,295. Also, using instances of the class, it's much easier to create separate threads of random numbers for a specific module.

 

Data Members:

public:

static const DWORD32 RAND_MAX

This definition is used to override the VC++ rand methods.

Methods:

public:

Prototype:

RandGenerator();

Remarks:

Constructor.

Prototype:

void srand(DWORD32 seed);

Remarks:

This method sets the starting point for generating a series of pseudorandom integers. 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 this method generates the same sequence as calling it with seed passed as 1.

Parameters:

DWORD32 seed

The starting seed.

Prototype:

DWORD32 rand(void);

Remarks:

This method returns a pseudorandom integer in the range 0 to RAND_MAX

Prototype:

int RandSign(void);

Remarks:

This method returns the random number sign, either -1 or 1.

Prototype:

float Rand01(void);

Remarks:

This method return a random number between 0.0f and 1.0f.

Prototype:

float Rand11(void);

Remarks:

This method return a random number between -1.0f and 1.0f.

Prototype:

float Rand55(void);

Remarks:

This method return a random number between -0.5f and 0.5f.

Prototype:

int Rand0X(void);

Remarks:

This method return a random number between 0 and maxnum.

Prototype:

bool Valid(void) const;

Remarks:

This method returns TRUE if the random number generator has been explicitly initialized by the srand() method.