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)) compatible with Stride math types.
- Sampling uniformly within 2D rectangles (NextPoint(Random, RectangleF)) and 3D bounding boxes (NextPoint(Random, BoundingBox)).
- 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)).
All methods validate the Random instance and use single-precision math to align with Stride's Vector2, Vector3, and Color types.
Methods
NextColor(Random)
Generates a random color.
public static Color NextColor(this Random random)
Parameters
Returns
- Color
A random color. Alpha is set to 255.
Exceptions
- ArgumentNullException
If the random argument is null.
NextDirection2D(Random)
Generates a random normalized 2D direction vector.
public static Vector2 NextDirection2D(this Random random)
Parameters
Returns
- Vector2
A unit-length (or zero) direction vector in the XY plane.
Exceptions
- ArgumentNullException
If the random argument is null.
NextDirection3D(Random)
Generates a random normalized 3D direction vector.
public static Vector3 NextDirection3D(this Random random)
Parameters
Returns
- Vector3
A unit-length (or zero) direction vector in 3D space.
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.
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.