Skip to main content

Cocos2D.CCFastRandom


Class CCFastRandom

Namespace: Cocos2D
Assembly: Cocos2D.dll, cocos2d_mono.Core.Android.dll, cocos2d_mono.Core.DesktopGL.dll, cocos2d-mono.Core.iOS.dll, cocos2d_mono.Core.Linux.dll, cocos2d_mono.Core.macOS.dll, cocos2d_mono.Windows.dll, cocos2d_mono.DesktopGL.dll, cocos2d-mono.iOS.dll, cocos2d_mono.Linux.dll, cocos2d_mono.macOS.dll

A fast random number generator for .NET Colin Green, January 2005

September 4th 2005 Added NextBytesUnsafe() - commented out by default. Fixed bug in Reinitialise() - y,z and w variables were not being reset.

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 8x 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.

public class CCFastRandom

Inheritance

objectCCFastRandom

Inherited Members

object.GetType(), object.MemberwiseClone(), object.ToString(), object.Equals(object?), object.Equals(object?, object?), object.ReferenceEquals(object?, object?), object.GetHashCode()

Constructors

CCFastRandom()

Initialises a new instance using time dependent seed.

public CCFastRandom()

CCFastRandom(int)

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

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

Initialises a new instance using time dependent seed.

public CCFastRandom()

CCFastRandom(int)

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

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

CCFastRandom()

public CCFastRandom()

CCFastRandom(int)

public CCFastRandom(int seed)

Parameters

seed int

Methods

Next()

Generates a random int over the range 0 to int.MaxValue-1. MaxValue is not generated in order to remain functionally equivalent to System.Random.Next(). This does slightly eat into some of the performance gain over System.Random, but not much. For better performance see:

Call NextInt() for an int over the range 0 to int.MaxValue.

Call NextUInt() and cast the result to an int to generate an int over the full Int32 value range including negative values.

public int Next()

Returns

int

Next(int)

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

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

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

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

Generates a random int over the range 0 to int.MaxValue-1. MaxValue is not generated in order to remain functionally equivalent to System.Random.Next(). This does slightly eat into some of the performance gain over System.Random, but not much. For better performance see:

Call NextInt() for an int over the range 0 to int.MaxValue.

Call NextUInt() and cast the result to an int to generate an int over the full Int32 value range including negative values.

public int Next()

Returns

int

Next(int)

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

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

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

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

Next()

public int Next()

Returns

int

Next(int)

public int Next(int upperBound)

Parameters

upperBound int

Returns

int

Next(int, int)

public int Next(int lowerBound, int upperBound)

Parameters

lowerBound int

upperBound int

Returns

int

NextBool()

Generates a single random bit. This method's performance is improved by generating 32 bits in one operation and storing them ready for future calls.

public bool NextBool()

Returns

bool

NextBool()

Generates a single random bit. This method's performance is improved by generating 32 bits in one operation and storing them ready for future calls.

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBool()

public bool NextBool()

Returns

bool

NextBytes(byte[])

Fills the provided byte array with random bytes. This method is functionally equivalent to System.Random.NextBytes().

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

Fills the provided byte array with random bytes. This method is functionally equivalent to System.Random.NextBytes().

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextBytes(byte[])

public void NextBytes(byte[] buffer)

Parameters

buffer byte[]

NextDouble()

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

public double NextDouble()

Returns

double

NextDouble()

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

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextDouble()

public double NextDouble()

Returns

double

NextInt()

Generates a random int over the range 0 to int.MaxValue, inclusive. This method differs from Next() only in that the range is 0 to int.MaxValue and not 0 to int.MaxValue-1.

The slight difference in range means this method is slightly faster than Next() but is not functionally equivalent to System.Random.Next().

public int NextInt()

Returns

int

NextInt()

Generates a random int over the range 0 to int.MaxValue, inclusive. This method differs from Next() only in that the range is 0 to int.MaxValue and not 0 to int.MaxValue-1.

The slight difference in range means this method is slightly faster than Next() but is not functionally equivalent to System.Random.Next().

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextInt()

public int NextInt()

Returns

int

NextUInt()

Generates a uint. Values returned are over the full range of a uint, uint.MinValue to uint.MaxValue, inclusive.

This is the fastest method for generating a single random number because the underlying random number generator algorithm generates 32 random bits that can be cast directly to a uint.

public uint NextUInt()

Returns

uint

NextUInt()

Generates a uint. Values returned are over the full range of a uint, uint.MinValue to uint.MaxValue, inclusive.

This is the fastest method for generating a single random number because the underlying random number generator algorithm generates 32 random bits that can be cast directly to a uint.

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

NextUInt()

public uint NextUInt()

Returns

uint

Reinitialise(int)

Reinitialises using an int value as a seed.

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

Reinitialises using an int value as a seed.

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

Reinitialise(int)

public void Reinitialise(int seed)

Parameters

seed int

  • Constructors
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
    • CCFastRandom()
    • CCFastRandom(int)
  • Methods
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • Next()
    • Next(int)
    • Next(int, int)
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBool()
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextBytes(byte[])
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextDouble()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • NextUInt()
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)
    • Reinitialise(int)