Skip to content

Instantly share code, notes, and snippets.

@maximecb
Created June 9, 2017 18:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save maximecb/617de45a99347a9911b1e0d974da5d62 to your computer and use it in GitHub Desktop.
Save maximecb/617de45a99347a9911b1e0d974da5d62 to your computer and use it in GitHub Desktop.
// Header files
#include "random.h"
// Initialize the constants for 64 bit random values
// Xn+1 = A * Xn + B (mod 2^64)
const uint64 CRandomGenerator::A_CONSTANT_64 = (uint64)(0x27BB2EE6)*(0x010000)*(0x010000) + (uint64)(0x87B0B0F);
const uint64 CRandomGenerator::B_CONSTANT_64 = 0xB504F32D;
// Initialize the constants for 32 bit random values
// Xn+1 = A * Xn + B (mod 2^32)
const uint32 CRandomGenerator::A_CONSTANT_32 = 0x0019660D;
const uint32 CRandomGenerator::B_CONSTANT_32 = 0x3C6EF35F;
// Initialize maximum integer values
const uint64 CRandomGenerator::MAX_INT_64 = (uint64)(0xFFFFFFFF)*(0x010000)*(0x010000) + (uint64)(0xFFFFFFFF);
const uint32 CRandomGenerator::MAX_INT_32 = (uint32)(0xFFFFFFFF);
// Initialize scaling values for random float generations
const float64 CRandomGenerator::FLOAT_SCALE_64 = (float64)1.0 / (float64)MAX_INT_64;
const float32 CRandomGenerator::FLOAT_SCALE_32 = (float32)1.0 / (float32)MAX_INT_32;
/*******************************************************
* Function: CRandomGenerator::CRandomGenerator()
* Purpose : Constructor for random number generator
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
CRandomGenerator::CRandomGenerator()
{
// Initialize the random seed to an arbitrary value
m_Seed.Int64 = 0;
m_Seed.Int32 = 1337;
}
/*******************************************************
* Function: CRandomGenerator::~CRandomGenerator()
* Purpose : Destructor for random number generator
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
CRandomGenerator::~CRandomGenerator()
{
}
/*******************************************************
* Function: CRandomGenerator::CRandomGenerator()
* Purpose : Constructor for random number generator
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
void CRandomGenerator::SetSeed(uint32 Seed)
{
// Set the new random seed
m_Seed.Int64 = 0;
m_Seed.Int32 = Seed;
}
/*******************************************************
* Function: CRandomGenerator::RandomInt64()
* Purpose : Generate a random 64 bit integer
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
uint64 CRandomGenerator::RandomInt64()
{
// Generate a new random integer
m_Seed.Int64 = m_Seed.Int64 * A_CONSTANT_64 + B_CONSTANT_64;
// Return the new random integer
return m_Seed.Int64;
}
/*******************************************************
* Function: CRandomGenerator::RandomInt32()
* Purpose : Generate a random 32 bit integer
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
uint32 CRandomGenerator::RandomInt32()
{
// Generate a new random integer
m_Seed.Int32 = m_Seed.Int32 * A_CONSTANT_32 + B_CONSTANT_32;
// Return the new random integer
return m_Seed.Int32;
}
/*******************************************************
* Function: CRandomGenerator::RandomFloat64()
* Purpose : Generate a random 64 bit float
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
float64 CRandomGenerator::RandomFloat64()
{
// Return a random floating-point value in the [0,1] range
return (float64)RandomInt64() * FLOAT_SCALE_64;
}
/*******************************************************
* Function: CRandomGenerator::RandomFloat32()
* Purpose : Generate a random 32 bit float
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
float32 CRandomGenerator::RandomFloat32()
{
// Return a random floating-point value in the [0,1] range
return (float32)RandomInt32() * FLOAT_SCALE_32;
}
/*******************************************************
* Class : CRandomGenerator
* Purpose : Seeded pseudo-random number generator
* Initial : Max Payne on July 19, 2005
********************************************************
Revisions and bug fixes:
*/
class CRandomGenerator
{
public:
// Constructor and destructor
CRandomGenerator();
~CRandomGenerator();
// Method to set a 32bit random seed
void SetSeed(uint32 Seed);
// Method to compute a 64bit random integer (random bit string)
uint64 RandomInt64();
// Method to compute a 32bit random integer (random bit string)
uint32 RandomInt32();
// Method to compute a 64bit random float in the [0, 1] range
float64 RandomFloat64();
// Method to compute a 32bit random float in the [0, 1] range
float32 RandomFloat32();
private:
// Constants for 64 bit random values
// Xn+1 = A * Xn + B (mod 2^64)
static const uint64 A_CONSTANT_64;
static const uint64 B_CONSTANT_64;
// Constants for 32 bit random values
// Xn+1 = A * Xn + B (mod 2^32)
static const uint32 A_CONSTANT_32;
static const uint32 B_CONSTANT_32;
// Maximum integer values
static const uint64 MAX_INT_64;
static const uint32 MAX_INT_32;
// Scaling values for random float generations
static const float64 FLOAT_SCALE_64;
static const float32 FLOAT_SCALE_32;
// Random seed union
union URandomSeed
{
// 64 bit random seed
uint64 Int64;
// 32 bit random seed
uint32 Int32;
};
// Random seed object
URandomSeed m_Seed;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment