2013-07-29 11:13:03 +00:00
|
|
|
|
|
|
|
// FastRandom.h
|
|
|
|
|
|
|
|
// Declares the cFastRandom class representing a fast random number generator
|
|
|
|
|
|
|
|
/*
|
|
|
|
The cFastRandom aims to provide a very fast, although not very cryptographically secure, random generator.
|
|
|
|
It is fast to instantiate, fast to query next, and partially multi-thread-safe.
|
|
|
|
It is multi-thread-safe in the sense that it can be accessed from multiple threads without crashing, but it may
|
|
|
|
yield duplicate numbers in that case.
|
|
|
|
|
|
|
|
Internally, this works similar to cNoise's integral noise generation, with some predefined inputs: the seed is
|
|
|
|
taken from a global counter and the random is calculated using a counter that is incremented on each use (hence
|
|
|
|
the multi-thread duplication). Two alternatives exists for each function, one that takes a range parameter,
|
|
|
|
and another that takes an additional "salt" parameter; this salt is used as an additional input to the random,
|
|
|
|
in order to avoid multi-thread duplication. If two threads both use the class at the same time with different
|
|
|
|
salts, the values they get will be different.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#pragma once
|
2014-10-19 13:10:18 +00:00
|
|
|
#include <random>
|
2013-07-29 11:13:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class cFastRandom
|
|
|
|
{
|
|
|
|
public:
|
2014-10-19 13:10:18 +00:00
|
|
|
|
2013-07-29 11:13:03 +00:00
|
|
|
cFastRandom(void);
|
|
|
|
|
2014-12-07 14:46:27 +00:00
|
|
|
/** Returns a random int in the range [0 .. a_Range - 1]; a_Range must be less than 1M */
|
2013-07-29 11:13:03 +00:00
|
|
|
int NextInt(int a_Range);
|
|
|
|
|
2014-12-07 14:46:27 +00:00
|
|
|
/** Returns a random float in the range [0 .. a_Range]; a_Range must be less than 1M */
|
2013-07-29 11:13:03 +00:00
|
|
|
float NextFloat(float a_Range);
|
|
|
|
|
2014-07-23 14:32:09 +00:00
|
|
|
/** Returns a random float between 0 and 1. */
|
2014-08-22 09:12:44 +00:00
|
|
|
float NextFloat(void) { return NextFloat(1); }
|
2014-07-23 14:32:09 +00:00
|
|
|
|
2014-04-07 18:41:47 +00:00
|
|
|
/** Returns a random int in the range [a_Begin .. a_End] */
|
2014-04-07 17:52:35 +00:00
|
|
|
int GenerateRandomInteger(int a_Begin, int a_End);
|
2013-07-29 11:13:03 +00:00
|
|
|
|
2014-10-19 13:10:18 +00:00
|
|
|
private:
|
|
|
|
|
|
|
|
std::minstd_rand m_LinearRand;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MTRand
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
MTRand(void);
|
|
|
|
|
2014-12-07 14:46:27 +00:00
|
|
|
/** Returns a random integer in the range [0 .. a_Range]. */
|
2014-10-19 13:10:18 +00:00
|
|
|
int randInt(int a_Range);
|
|
|
|
|
2014-12-07 14:46:27 +00:00
|
|
|
/** Returns a random integer in the range [0 .. MAX_INT]. */
|
2014-10-19 13:10:18 +00:00
|
|
|
int randInt(void);
|
2013-07-29 11:13:03 +00:00
|
|
|
|
2014-12-07 14:46:27 +00:00
|
|
|
/** Returns a random floating point number in the range [0 .. a_Range]. */
|
2014-10-19 13:10:18 +00:00
|
|
|
double rand(double a_Range);
|
2013-07-29 11:13:03 +00:00
|
|
|
|
2014-10-19 13:10:18 +00:00
|
|
|
private:
|
2013-07-29 11:13:03 +00:00
|
|
|
|
2014-10-19 13:10:18 +00:00
|
|
|
std::mt19937 m_MersenneRand;
|
2014-10-21 13:21:08 +00:00
|
|
|
};
|
2014-12-07 14:46:27 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|