Class Random
Random number generator service.
Implements
Inherited Members
Namespace: Evergine.Framework.Services
Assembly: Evergine.Framework.dll
Syntax
public sealed class Random : Service, IDependencyObject
Constructors
Random()
Initializes a new instance of the Random class.
Declaration
public Random()
Remarks
The initial seed depends on the time.
Random(int)
Initializes a new instance of the Random class with a given seed.
Declaration
public Random(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
CreateStrongRandomSeed()
Creates a strong random number to use as a seed. Do not use it as an integer random generator because it is slow. Use with caution.
Declaration
public int CreateStrongRandomSeed()
Returns
Type | Description |
---|---|
int | A random integer to be used as a seed. |
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 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. |
Next(int)
Generates a random int in 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, excluding 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
This method's performance is improved by generating 32 bits in one operation and storing them for future calls.
NextBool(float)
Generates a single random bit with a specified 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. |
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 int over the range 0 to int.MaxValue-1. Same as Next() method.
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 -1. |
NextRotation()
Generates a single angle in radians [0 to 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, 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, uniformly distributed.
Declaration
public Vector3 OnUnitSphere()
Returns
Type | Description |
---|---|
Vector3 | A Vector3D. |