C# Class NSoft.NFramework.Numerics.FastRandom

A fast random number generator for .NET Colin Green, January 2005 Key points: 1) Based on a simple and fast xor-shift pseudo random number generator (RNG) specified in: Marsaglia, George. (2003). Xorshift RNGs. http://www.jstatsoft.org/v08/i14/xorshift.pdf This particular implementation of xorshift has a period of 2^128-1. See the above paper to see how this can be easily extened if you need a longer period. At the time of writing I could find no information on the period of System.Random for comparison. 2) Faster than System.Random. Up to 15x faster, depending on which methods are called. 3) Direct replacement for System.Random. This class implements all of the methods that System.Random does plus some additional methods. The like named methods are functionally equivalent. 4) Allows fast re-initialisation with a seed, unlike System.Random which accepts a seed at construction time which then executes a relatively expensive initialisation routine. This provides a vast speed improvement if you need to reset the pseudo-random number sequence many times, e.g. if you want to re-generate the same sequence many times. An alternative might be to cache random numbers in an array, but that approach is limited by memory capacity and the fact that you may also want a large number of different sequences cached. Each sequence can each be represented by a single seed value (int) when using FastRandom. Notes. A further performance improvement can be obtained by declaring local variables as static, thus avoiding re-allocation of variables on each call. However care should be taken if multiple instances of FastRandom are in use or if being used in a multi-threaded environment.
Exibir arquivo Open project: debop/NFramework

Public Methods

Method Description
FastRandom ( ) : System

Initialises a new instance using time dependent seed.

FastRandom ( int seed ) : System

Initialises a new instance using an int value as seed. This constructor signature is provided to maintain compatibility with System.Random

Next ( ) : int

Generates a random int. Values returned are over the range 0 to int.MaxValue-1. MaxValue is not generated to remain functionally equivalent to System.Random.Next(). If you require an int from the full range, including negative values then call NextUint() and cast the value to an int.

Next ( int upperBound ) : int

Generates a random int over the range 0 to upperBound-1, and not including upperBound.

Next ( int lowerBound, int upperBound ) : int

Generates a random int over the range lowerBound to upperBound-1, and not including upperBound. upperBound must be >= lowerBound. lowerBound may be negative.

NextBool ( ) : bool

Generates random bool. Increased performance is achieved by buffering 32 random bits for future calls. Thus the random number generator is only invoked once in every 32 calls.

NextBytes ( byte buffer ) : void

Fills the provided byte array with random bytes. Increased performance is achieved by dividing and packaging bits directly from the random number generator and storing them in 4 byte 'chunks'.

NextDouble ( ) : double

Generates a random double. Values returned are from 0.0 up to but not including 1.0.

Reinitialize ( int seed ) : void

Reinitializes using an int value as a seed.

Private Methods

Method Description
NextUInt ( ) : uint

Method Details

FastRandom() public method

Initialises a new instance using time dependent seed.
public FastRandom ( ) : System
return System

FastRandom() public method

Initialises a new instance using an int value as seed. This constructor signature is provided to maintain compatibility with System.Random
public FastRandom ( int seed ) : System
seed int
return System

Next() public method

Generates a random int. Values returned are over the range 0 to int.MaxValue-1. MaxValue is not generated to remain functionally equivalent to System.Random.Next(). If you require an int from the full range, including negative values then call NextUint() and cast the value to an int.
public Next ( ) : int
return int

Next() public method

Generates a random int over the range 0 to upperBound-1, and not including upperBound.
public Next ( int upperBound ) : int
upperBound int
return int

Next() public method

Generates a random int over the range lowerBound to upperBound-1, and not including upperBound. upperBound must be >= lowerBound. lowerBound may be negative.
public Next ( int lowerBound, int upperBound ) : int
lowerBound int
upperBound int
return int

NextBool() public method

Generates random bool. Increased performance is achieved by buffering 32 random bits for future calls. Thus the random number generator is only invoked once in every 32 calls.
public NextBool ( ) : bool
return bool

NextBytes() public method

Fills the provided byte array with random bytes. Increased performance is achieved by dividing and packaging bits directly from the random number generator and storing them in 4 byte 'chunks'.
public NextBytes ( byte buffer ) : void
buffer byte
return void

NextDouble() public method

Generates a random double. Values returned are from 0.0 up to but not including 1.0.
public NextDouble ( ) : double
return double

Reinitialize() public method

Reinitializes using an int value as a seed.
public Reinitialize ( int seed ) : void
seed int
return void