Class RandomExtensions
- Namespace
- Stride.CommunityToolkit.Mathematics
- Assembly
- Stride.CommunityToolkit.dll
Helper extension methods for Random tailored for gameplay and procedural content scenarios.
public static class RandomExtensions
- Inheritance
-
RandomExtensions
Remarks
Provides convenience APIs for:
- Generating scalar values (NextSingle(Random), NextSingle(Random, float, float)) compatible with Stride math types.
- Sampling uniformly within 2D rectangles (NextPoint(Random, RectangleF)), 3D bounding boxes (NextPoint(Random, BoundingBox)) and component ranges (NextVector3(Random, Vector3, Vector3)).
- Producing random direction vectors in 2D/3D (NextDirection2D(Random), NextDirection3D(Random)).
- Sampling a point uniformly inside a circle (PointInACircle(Random, float)) using area-correct square–root radial distribution.
- Creating a random opaque color (NextColor(Random, ColorTone)), vivid by default or in another ColorTone.
All methods validate the Random instance and use single-precision math to align with Stride's Vector2, Vector3, and Color types.
They extend the caller's own Random, so a world built from new Random(seed) comes out the same for the same seed.
Methods
NextColor(Random, ColorTone)
Generates a random opaque colour: a hue spread evenly round the wheel, with the saturation and
brightness drawn from the range tone names.
public static Color NextColor(this Random random, ColorTone tone = ColorTone.Vivid)
Parameters
randomRandomAn instance of Random.
toneColorToneThe part of the colour space to draw from. Defaults to Vivid.
Returns
- Color
A random colour with alpha 255.
Exceptions
- ArgumentNullException
If the random argument is null.
NextDirection2D(Random)
Generates a random unit direction in the XY plane, every angle equally likely.
public static Vector2 NextDirection2D(this Random random)
Parameters
Returns
- Vector2
A unit-length direction vector in the XY plane.
Exceptions
- ArgumentNullException
If the random argument is null.
NextDirection3D(Random)
Generates a random unit direction in 3D, every direction equally likely.
public static Vector3 NextDirection3D(this Random random)
Parameters
Returns
- Vector3
A unit-length direction vector.
Exceptions
- ArgumentNullException
If the random argument is null.
NextPoint(Random, BoundingBox)
Generates a random point in 3D space within the specified region.
public static Vector3 NextPoint(this Random random, BoundingBox region)
Parameters
randomRandomAn instance of Random.
regionBoundingBoxA 3D region in which point is generated.
Returns
- Vector3
A random point whose X/Y/Z components lie within
region's bounds.
Exceptions
- ArgumentNullException
If the random argument is null.
NextPoint(Random, RectangleF)
Generates a random point in 2D space within the specified region.
public static Vector2 NextPoint(this Random random, RectangleF region)
Parameters
randomRandomAn instance of Random.
regionRectangleFA 2D region in which point is generated.
Returns
- Vector2
A random point in 2D space within the specified region.
Exceptions
- ArgumentNullException
If the random argument is null.
NextSingle(Random)
Generates a random float.
public static float NextSingle(this Random random)
Parameters
Returns
Exceptions
- ArgumentNullException
If the random argument is null.
NextSingle(Random, float, float)
Generates a random float between min and max.
public static float NextSingle(this Random random, float min, float max)
Parameters
randomRandomAn instance of Random.
minfloatThe lowest value returned.
maxfloatThe value the range runs up to.
Returns
Exceptions
- ArgumentNullException
If the random argument is null.
NextVector3(Random, Vector3, Vector3)
Generates a random point with each component between the matching components of min and max.
public static Vector3 NextVector3(this Random random, Vector3 min, Vector3 max)
Parameters
randomRandomAn instance of Random.
minVector3The lowest X, Y and Z.
maxVector3The highest X, Y and Z.
Returns
- Vector3
A random point inside the box the two corners span.
Exceptions
- ArgumentNullException
If the random argument is null.
NextVector3(Random, float, float)
Generates a random vector with every component between min and max.
public static Vector3 NextVector3(this Random random, float min, float max)
Parameters
randomRandomAn instance of Random.
minfloatThe lowest value of each component.
maxfloatThe highest value of each component.
Returns
- Vector3
A random vector inside the cube the range spans.
Exceptions
- ArgumentNullException
If the random argument is null.
PointInACircle(Random, float)
Generates a random point uniformly distributed inside a circle of the given radius.
public static Vector2 PointInACircle(this Random random, float radius = 1)
Parameters
Returns
- Vector2
A random point whose distance from the origin is <=
radius.
Exceptions
- ArgumentNullException
If the random argument is null.