Logo
~Sockets~
~Examples~
~Contact~


RandomNumber Class Reference

The following class uses an xorshift algorithm proposed in the following paper: More...

#include <RandomNumber.h>

List of all members.


Public Member Functions

 RandomNumber (bool time_shuffle=false)
 Default constructor.
 RandomNumber (unsigned long int x_seed, unsigned long int y_seed, unsigned long int z_seed, unsigned long int w_seed)
 Custom constructor.
 ~RandomNumber ()
 Destructor.
void reset ()
 Reset internal state to initial seed values.
 operator unsigned long int () const
 Cast operator to obtain current random value in the PRNG.
unsigned long int next ()
 Go to the next number in the PRNG sequence.
unsigned long int skip (unsigned long int s)
 Skip ahead in the PRNG sequence by a given number of iterations.
void getSeed (unsigned long int &x_seed, unsigned long int &y_seed, unsigned long int &z_seed, unsigned long int &w_seed)
 Obtain all the initial seeds for this PRNG.

Static Public Member Functions

static unsigned long int max_random ()
 Get the maximum possible random number from this PRNG.

Static Public Attributes

static const unsigned long int X_SEED_DEFAULT = 123456789UL
 Default x-seed as proposed by the paper.
static const unsigned long int Y_SEED_DEFAULT = 362436069UL
 Default y-seed as proposed by the paper.
static const unsigned long int Z_SEED_DEFAULT = 521288629UL
 Default z-seed as proposed by the paper.
static const unsigned long int W_SEED_DEFAULT = 88675123UL
 Default w-seed as proposed by the paper.

Private Attributes

unsigned long int mXSeed
 X seed.
unsigned long int mYSeed
 Y seed.
unsigned long int mZSeed
 Z seed.
unsigned long int mWSeed
 W seed.
unsigned long int mX
 X value.
unsigned long int mY
 Y value.
unsigned long int mZ
 Z value.
unsigned long int mW
 W value.

Detailed Description

The following class uses an xorshift algorithm proposed in the following paper:

The algorithm provides a PRNG with a period of (2^128)-1

This PRNG is *not* intended for cryptographic purposes

Definition at line 40 of file RandomNumber.h.


Constructor & Destructor Documentation

RandomNumber::RandomNumber ( bool  time_shuffle = false  ) 

Default constructor.

NOTE: Internal seeds are set to defaults proposed by the paper

Definition at line 34 of file RandomNumber.cpp.

References reset().

00035 :mXSeed(time_shuffle ? (unsigned long)time(NULL) ^ X_SEED_DEFAULT : X_SEED_DEFAULT)
00036 ,mYSeed(time_shuffle ? (unsigned long)time(NULL) ^ Y_SEED_DEFAULT : Y_SEED_DEFAULT)
00037 ,mZSeed(time_shuffle ? (unsigned long)time(NULL) ^ Z_SEED_DEFAULT : Z_SEED_DEFAULT)
00038 ,mWSeed(time_shuffle ? (unsigned long)time(NULL) ^ W_SEED_DEFAULT : W_SEED_DEFAULT)
00039 {
00040   reset();
00041 }

RandomNumber::RandomNumber ( unsigned long int  x_seed,
unsigned long int  y_seed,
unsigned long int  z_seed,
unsigned long int  w_seed 
)

Custom constructor.

Parameters:
x_seed X seed
y_seed Y seed
z_seed Z seed
w_seed W seed

Definition at line 43 of file RandomNumber.cpp.

References reset().

00048 :mXSeed(x_seed)
00049 ,mYSeed(y_seed)
00050 ,mZSeed(z_seed)
00051 ,mWSeed(w_seed)
00052 {
00053   reset();
00054 }

RandomNumber::~RandomNumber (  ) 

Destructor.

Definition at line 56 of file RandomNumber.cpp.

00057 {
00058 }


Member Function Documentation

void RandomNumber::reset (  ) 

Reset internal state to initial seed values.

Definition at line 60 of file RandomNumber.cpp.

References mW, mWSeed, mX, mXSeed, mY, mYSeed, mZ, and mZSeed.

Referenced by RandomNumber().

00061 {
00062   mX = mXSeed;
00063   mY = mYSeed;
00064   mZ = mZSeed;
00065   mW = mWSeed;
00066 }

RandomNumber::operator unsigned long int (  )  const

Cast operator to obtain current random value in the PRNG.

Returns:
Current random value in the PRNG

Definition at line 68 of file RandomNumber.cpp.

References mW.

00069 {
00070   return(mW);
00071 }

unsigned long int RandomNumber::next (  ) 

Go to the next number in the PRNG sequence.

NOTE: This method is a slightly modified implementation of the xor128() function proposed in the paper

Returns:
Next value produced by the PRNG (after updating)

Definition at line 73 of file RandomNumber.cpp.

References mW, mX, mY, and mZ.

Referenced by skip(), and Utility::u2ip().

00074 {
00075   register unsigned long int t = (mX ^ (mX<<11));
00076 
00077   mX = mY;
00078 
00079   mY = mZ;
00080 
00081   mZ = mW;
00082 
00083   return(mW = (mW ^ (mW>>19)) ^ (t ^ (t>>8)));
00084 }

unsigned long int RandomNumber::skip ( unsigned long int  s  ) 

Skip ahead in the PRNG sequence by a given number of iterations.

Parameters:
s Number of iterations to skip ahead
Returns:
Value produced by the PRNG after skipping ahead in the sequence

Definition at line 86 of file RandomNumber.cpp.

References mW, and next().

00087 {
00088   for(register unsigned long int i = 0 ; i < s ; ++i)
00089   {
00090     (void)next();
00091   }
00092 
00093   return(mW);
00094 }

void RandomNumber::getSeed ( unsigned long int &  x_seed,
unsigned long int &  y_seed,
unsigned long int &  z_seed,
unsigned long int &  w_seed 
)

Obtain all the initial seeds for this PRNG.

Parameters:
x_seed X seed (output)
y_seed Y seed (output)
z_seed Z seed (output)
w_seed W seed (output)

Definition at line 96 of file RandomNumber.cpp.

References mWSeed, mXSeed, mYSeed, and mZSeed.

00101 {
00102   x_seed = mXSeed;
00103   y_seed = mYSeed;
00104   z_seed = mZSeed;
00105   w_seed = mWSeed;
00106 }

unsigned long int RandomNumber::max_random (  )  [static]

Get the maximum possible random number from this PRNG.

Returns:
Maximum possible random number from this PRNG

Definition at line 108 of file RandomNumber.cpp.

00109 {
00110   return(std::numeric_limits<unsigned long int>::max());
00111 }


Member Data Documentation

const unsigned long int RandomNumber::X_SEED_DEFAULT = 123456789UL [static]

Default x-seed as proposed by the paper.

Date:
September 2006

Definition at line 135 of file RandomNumber.h.

const unsigned long int RandomNumber::Y_SEED_DEFAULT = 362436069UL [static]

Default y-seed as proposed by the paper.

Definition at line 140 of file RandomNumber.h.

const unsigned long int RandomNumber::Z_SEED_DEFAULT = 521288629UL [static]

Default z-seed as proposed by the paper.

Definition at line 145 of file RandomNumber.h.

const unsigned long int RandomNumber::W_SEED_DEFAULT = 88675123UL [static]

Default w-seed as proposed by the paper.

Definition at line 150 of file RandomNumber.h.

unsigned long int RandomNumber::mXSeed [private]

X seed.

Definition at line 156 of file RandomNumber.h.

Referenced by getSeed(), and reset().

unsigned long int RandomNumber::mYSeed [private]

Y seed.

Definition at line 161 of file RandomNumber.h.

Referenced by getSeed(), and reset().

unsigned long int RandomNumber::mZSeed [private]

Z seed.

Definition at line 166 of file RandomNumber.h.

Referenced by getSeed(), and reset().

unsigned long int RandomNumber::mWSeed [private]

W seed.

Definition at line 171 of file RandomNumber.h.

Referenced by getSeed(), and reset().

unsigned long int RandomNumber::mX [private]

X value.

Definition at line 176 of file RandomNumber.h.

Referenced by next(), and reset().

unsigned long int RandomNumber::mY [private]

Y value.

Definition at line 181 of file RandomNumber.h.

Referenced by next(), and reset().

unsigned long int RandomNumber::mZ [private]

Z value.

Definition at line 186 of file RandomNumber.h.

Referenced by next(), and reset().

unsigned long int RandomNumber::mW [private]

W value.

NOTE: This is the externally-visible next value produced by the PRNG

Definition at line 193 of file RandomNumber.h.

Referenced by next(), operator unsigned long int(), reset(), and skip().


The documentation for this class was generated from the following files:
Page, code, and content Copyright (C) 2007 by Anders Hedström
Generated for C++ Sockets by  doxygen 1.4.4