Class FastRandom
Random number generator service (based on XOR shift technique).
Implements
Inherited Members
Namespace: Evergine.Framework.Services
Assembly: Evergine.Framework.dll
Syntax
public sealed class FastRandom : Service, IDependencyObject
Remarks
Based on FastRandom.cs by colgreen: http://www.codeproject.com/Articles/9187/A-fast-equivalent-for-System-Random.
Constructors
FastRandom()
Initializes a new instance of the FastRandom class.
Declaration
public FastRandom()
Remarks
The initial seed depends on the time.
FastRandom(int)
Initializes a new instance of the FastRandom class with a given seed.
Declaration
public FastRandom(int seed)
Parameters
Type | Name | Description |
---|---|---|
int | seed | The initial seed. |
Properties
Seed
Gets or sets a value indicating whether the seed is enabled.
Declaration
public int Seed { get; set; }
Property Value
Type | Description |
---|---|
int |
Methods
InUnitSphere()
Generates a 3D vector inside a radius 1 sphere with a uniform distribution.
Declaration
public Vector3 InUnitSphere()
Returns
Type | Description |
---|---|
Vector3 | A Vector3D. |
InsideUnitCircle()
Generates a 2D vector inside a unit circle with a uniform distribution.
Declaration
public Vector2 InsideUnitCircle()
Returns
Type | Description |
---|---|
Vector2 | A Vector2D. |
Next()
Generates a random int over the range 0 to int.MaxValue-1.
Declaration
public int Next()
Returns
Type | Description |
---|---|
int | A 32-bit signed integer greater than or equal to zero and less than MaxValue. |
Remarks
MaxValue is not generated to remain functionally equivalent to System.Random.Next(). This slightly diminishes some of the performance gain over System.Random, but not significantly. 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.
Next(int)
Generates a random integer within the range 0 to upperBound-1, not including upperBound.
Declaration
public int Next(int upperBound)
Parameters
Type | Name | Description |
---|---|---|
int | upperBound | Non-inclusive upper bound. |
Returns
Type | Description |
---|---|
int | A 32-bit signed integer greater than or equal to zero and less than |
Next(int, int)
Generates a random int over the range lowerBound to upperBound-1, not including upperBound. upperBound must be >= lowerBound. lowerBound may be negative.
Declaration
public int Next(int lowerBound, int upperBound)
Parameters
Type | Name | Description |
---|---|---|
int | lowerBound | The inclusive lower bound. |
int | upperBound | The non-inclusive upper bound. |
Returns
Type | Description |
---|---|
int | A 32-bit signed integer greater than or equal to |
NextBool()
Generates a single random bit.
Declaration
public bool NextBool()
Returns
Type | Description |
---|---|
bool | A boolean. |
Remarks
The performance of this method is improved by generating 32 bits in one operation and storing them for future calls.
NextBool(float)
Generates a single random bit with a true value weight.
Declaration
public bool NextBool(float trueWeight)
Parameters
Type | Name | Description |
---|---|---|
float | trueWeight | The weight of the true value. |
Returns
Type | Description |
---|---|
bool | A boolean. |
NextBytes(byte[])
Fills the provided byte array with random bytes.
Declaration
public void NextBytes(byte[] buffer)
Parameters
Type | Name | Description |
---|---|---|
byte[] | buffer | An array of bytes to contain random numbers. |
Remarks
This method is functionally equivalent to System.Random.NextBytes().
NextDouble()
Generates a random double between 0.0 and 1.0, not including 1.0.
Declaration
public double NextDouble()
Returns
Type | Description |
---|---|
double | A double-precision floating-point number greater than or equal to 0.0 and less than 1.0. |
NextInt()
Generates a random integer over the range 0 to int.MaxValue, both inclusive.
Declaration
public int NextInt()
Returns
Type | Description |
---|---|
int | A 32-bit signed integer greater than or equal to zero and less than or equal to MaxValue. |
Remarks
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().
NextRotation()
Generates a single angle in radians [0-2*Pi).
Declaration
public float NextRotation()
Returns
Type | Description |
---|---|
float | An angle. |
NextUInt()
Generates a uint. Values returned are over the full range of a uint, from uint.MinValue to uint.MaxValue, inclusive.
Declaration
public uint NextUInt()
Returns
Type | Description |
---|---|
uint | A 32-bit unsigned integer greater than or equal to zero and less than or equal to MaxValue. |
Remarks
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.
OnUnitSphere()
Generates a 3D vector on the surface of a sphere with a radius of 1, featuring a uniform distribution.
Declaration
public Vector3 OnUnitSphere()
Returns
Type | Description |
---|---|
Vector3 | A Vector3D. |