Rudiments
|
#include <randomnumber.h>
Inherits object.
Public Member Functions | |
randomnumber () | |
~randomnumber () | |
bool | setSeed (uint32_t seed) |
bool | generate (uint32_t *result) |
bool | generate (int32_t *result, int32_t lower, int32_t upper) |
Public Member Functions inherited from object | |
virtual | ~object () |
Static Public Member Functions | |
static uint32_t | getSeed () |
static uint32_t | generate (uint32_t seed) |
static int32_t | generate (uint32_t seed, int32_t lower, int32_t upper) |
static int32_t | scale (uint32_t number, int32_t lower, int32_t upper) |
static uint32_t | getRandMax () |
static bool | getNeedsMutex () |
static void | setMutex (threadmutex *mtx) |
The randomnumber class provides methods for generating and scaling random numbers.
Superfluous background:
There is no such thing as true randomness. Many random number generators just return values from a highly divergent series. Others collect "entropy" from system hardware returning numbers that are actually associated with system activity but in an indiscernible way. In most cases, entropy-based systems are just used to seed systems that return values from a highly divergnet series.
If there is only one running process generating random numbers using a highly divergent series, successive calls to generate() will never return the same value until all numbers between 0 and the number returned by getRandMax() (usually 2^31-1 but could be 2^32-1 or 2^15-1 on some systems) have been returned. At that point, the entire sequence will repeat.
Calls to generate() with lower/upper parameters may return the same value before all numbers in the range have been returned because it just scales the result of generate() without lower/upper parameters which operates on a larger range.
randomnumber::randomnumber | ( | ) |
Creates a new instance of the randomnumber class.
randomnumber::~randomnumber | ( | ) |
Deletes this instance of the randomnumber class.
Generates a random number between "lower" and "upper" and sets "result" to this number.
Returns true on success and false on failure.
Generates a random number between 0 and the number given by getRandMax() (usually 2^31-1 but could be 2^32-1 or 2^15-1) and sets "result" to this number.
Returns true on success and false on failure.
Generates a random number between 0 and 2^32, based on seed, and returns it.
It is ok to use the result of this method as the seed for the next number.
Generates a random number between 0 and the number given by getRandMax() (usually 2^31-1 but could be 2^32-1 or 2^15-1), based on "seed", scales that value to be between "lower" and "upper" and returns it.
It is NOT ok to use the result of this method as the seed for the next number.
Returns true if this class needs a mutex to operate safely in a threaded environment and false otherwise.
Returns the largest number that generate() could possibly generate. This is usually 2^31-1 on 32 or 64 bit machines but there are exceptions where it is 2^32-1 or 2^15-1.
Generates a random number seed by first attempting to get one from /dev/urandom and if that fails, getting the number of seconds since 1970.
NOTE: If your application is multi-threaded, /dev/urandom is not accessible, and separate threads can call getSeed(), unsynchronized, then be sure to give the datetime and environment classes individual mutexes using datetime::setMutex() and environemnt::setMutex(), as on some platforms, getting the number of seconds since 1970 involves calling non-thread safe date/time-related functions as well as getting, and possibly setting, the TZ environment variable.
Scales "number" to be between "lower" and "upper" and returns it.
It is NOT ok to use the result of this method as the seed for the next number.
|
static |
Allows you to supply a mutex is the class needs it (see getNeedsMutex()). If your application is not multithreaded, then there is no need to supply a mutex.