3ds Max C++ API Reference
Random Class Reference

#include <random.h>

+ Inheritance diagram for Random:

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
 

Additional Inherited Members

- Static Public Member Functions inherited from MaxHeapOperators
static UtilExport voidoperator new (size_t size)
 Standard new operator used to allocate objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e)
 Standard new operator used to allocate objects if there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate objects that takes the type of memory, filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate arrays of objects. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport void operator delete (void *ptr)
 Standard delete operator used to deallocate an object If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an object If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an object that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, void *placement_ptr)
 Placement new operator. More...
 
static UtilExport void operator delete (void *ptr, void *placement_ptr)
 Placement delete operator. More...
 
static UtilExport voidaligned_malloc (size_t size, size_t alignment)
 Allocates memory on a specified alignment boundary. More...
 
static UtilExport voidaligned_realloc (void *ptr, size_t size, size_t alignment)
 Reallocates memory on a specified alignment boundary. More...
 
static UtilExport void aligned_free (void *ptr)
 Frees a block of memory that was allocated with aligned_malloc/aligned_realloc. More...
 

Detailed 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); // 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
...
static UtilExport const int s_rand_max
Definition: random.h:84
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.

Constructor & Destructor Documentation

◆ Random()

Remarks
The constructor will automatically initialize the seed.

Member Function Documentation

◆ srand()

UtilExport 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.

◆ rand()

UtilExport 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.

◆ get()

int get ( int  max_exclusive = s_rand_max+1,
int  min_inclusive = 0 
)
inline
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.
96  {
97  return (this->rand() % (max_exclusive - min_inclusive) + min_inclusive);
98  }
UtilExport int rand()

◆ getf()

float getf ( float  max_exclusive = 1.0f,
float  min_inclusive = 0.0f 
)
inline
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.
110  {
111  return (this->rand() / (s_rand_max+1.0f) * (max_exclusive - min_inclusive) + min_inclusive);
112  }

Member Data Documentation

◆ s_rand_max

UtilExport const int s_rand_max
static