Table of Contents

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:

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

random Random

An instance of Random.

tone ColorTone

The 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

random Random

An instance of Random.

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

random Random

An instance of Random.

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

random Random

An instance of Random.

region BoundingBox

A 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

random Random

An instance of Random.

region RectangleF

A 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

random Random

An instance of Random.

Returns

float

A random float.

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

random Random

An instance of Random.

min float

The lowest value returned.

max float

The value the range runs up to.

Returns

float

A random float in the range.

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

random Random

An instance of Random.

min Vector3

The lowest X, Y and Z.

max Vector3

The 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

random Random

An instance of Random.

min float

The lowest value of each component.

max float

The 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

random Random

An instance of Random.

radius float

Radius of circle. Default 1.0f.

Returns

Vector2

A random point whose distance from the origin is <= radius.

Exceptions

ArgumentNullException

If the random argument is null.