RandomExtensions Class

Provides extension methods for Random to generate various types of random values.

public static class RandomExtensions

Methods

NameDescription
NextBoolean(Random)Returns a random boolean value.
NextByte(Random)Returns a random byte value, from Byte.MinValue to Byte.MaxValue, both included.
NextByte(Random, byte, byte)Returns a random byte value greater than or equal to min and less than max.
NextDateTime(Random, DateTime, DateTime)Returns a random DateTime value greater than or equal to min and less than max.
NextDecimal(Random, decimal, decimal)Returns a random decimal value greater than or equal to min and less than max.
NextDouble(Random, double, double)Returns a random double value greater than or equal to min and less than max.
NextFromList<T>(Random, ICollection<T>)Returns a random element from a collection.
NextFromList<T>(Random, IList<T>)Returns a random element from a list.
NextFromList<T>(Random, IReadOnlyCollection<T>)Returns a random element from a read-only collection.
NextFromList<T>(Random, IReadOnlyList<T>)Returns a random element from a read-only list.
NextFromList<T>(Random, ReadOnlyMemory<T>)Returns a random element from a memory.
NextFromList<T>(Random, ReadOnlySpan<T>)Returns a random element from a span.
NextFromList<T>(Random, T[])Returns a random element from an array.
NextInt16(Random)Returns a random 16-bit signed integer, from Int16.MinValue to Int16.MaxValue, both included.
NextInt16(Random, short, short)Returns a random 16-bit signed integer greater than or equal to min and less than max.
NextInt32(Random, int, int)Returns a random 32-bit signed integer within the specified range.
NextInt64(Random, long, long)Returns a random 64-bit signed integer greater than or equal to min and less than max.
NextSByte(Random)Returns a random signed byte value, from SByte.MinValue to SByte.MaxValue, both included.
NextSByte(Random, sbyte, sbyte)Returns a random signed byte value greater than or equal to min and less than max.
NextSingle(Random, float, float)Returns a random single-precision floating point value greater than or equal to min and less than max.
NextString(Random, int, int, string)Returns a random string with length between the specified range using characters from the provided character set.
NextString(Random, int, string)Returns a random string of the specified length using characters from the provided character set.
NextUInt16(Random)Returns a random 16-bit unsigned integer, from UInt16.MinValue to UInt16.MaxValue, both included.
NextUInt16(Random, ushort, ushort)Returns a random 16-bit unsigned integer greater than or equal to min and less than max.
NextUInt32(Random)Returns a random 32-bit unsigned integer, from UInt32.MinValue to UInt32.MaxValue, both included.
NextUInt32(Random, uint, uint)Returns a random 32-bit unsigned integer greater than or equal to min and less than max.
NextUInt64(Random)Returns a random 64-bit unsigned integer, from UInt64.MinValue to UInt64.MaxValue, both included.
NextUInt64(Random, ulong, ulong)Returns a random 64-bit unsigned integer greater than or equal to min and less than max.

NextBoolean(Random)

RandomExtensions.cs

Returns a random boolean value.

public static bool NextBoolean(this Random random)

Parameters

random Random

Returns

bool

NextByte overloads

NextByte(Random)

RandomExtensions.cs

Returns a random byte value, from Byte.MinValue to Byte.MaxValue, both included.

public static byte NextByte(this Random random)
Parameters
random Random
Returns

byte

NextByte(Random, byte, byte)

RandomExtensions.cs

Returns a random byte value greater than or equal to min and less than max.

public static byte NextByte(this Random random, byte min, byte max)
Parameters
random Random
min byte
max byte
Returns

byte

NextDateTime(Random, DateTime, DateTime)

RandomExtensions.cs

Returns a random DateTime value greater than or equal to min and less than max.

public static DateTime NextDateTime(this Random random, DateTime min, DateTime max)

Parameters

random Random
min DateTime
max DateTime

Returns

DateTime

Remarks

The result has the DateTime.Kind of min.

NextDecimal(Random, decimal, decimal)

RandomExtensions.cs

Returns a random decimal value greater than or equal to min and less than max.

public static decimal NextDecimal(this Random random, decimal min = -79228162514264337593543950335m, decimal max = 79228162514264337593543950335m)

Parameters

random Random
min decimal
max decimal

Returns

decimal

Remarks

The value is derived from Random.NextDouble, so it carries the granularity of a double rather than that of a decimal.

NextDouble(Random, double, double)

RandomExtensions.cs

Returns a random double value greater than or equal to min and less than max.

public static double NextDouble(this Random random, double min = 0d, double max = 1d)

Parameters

random Random
min double
max double

Returns

double

NextFromList overloads

NextFromList<T>(Random, ICollection<T>)

RandomExtensions.cs

Returns a random element from a collection.

public static T NextFromList<T>(this Random random, ICollection<T> list)
Type parameters
T
Parameters
random Random
list ICollection<T>
Returns

T

Remarks

When the collection is not a list, the element is found by enumerating the collection.

NextFromList<T>(Random, IList<T>)

RandomExtensions.cs

Returns a random element from a list.

public static T NextFromList<T>(this Random random, IList<T> list)
Type parameters
T
Parameters
random Random
list IList<T>
Returns

T

NextFromList<T>(Random, IReadOnlyCollection<T>)

RandomExtensions.cs

Returns a random element from a read-only collection.

public static T NextFromList<T>(this Random random, IReadOnlyCollection<T> list)
Type parameters
T
Parameters
random Random
list IReadOnlyCollection<T>
Returns

T

Remarks

When the collection is not a list, the element is found by enumerating the collection.

NextFromList<T>(Random, IReadOnlyList<T>)

RandomExtensions.cs

Returns a random element from a read-only list.

public static T NextFromList<T>(this Random random, IReadOnlyList<T> list)
Type parameters
T
Parameters
random Random
list IReadOnlyList<T>
Returns

T

NextFromList<T>(Random, ReadOnlyMemory<T>)

RandomExtensions.cs

Returns a random element from a memory.

public static T NextFromList<T>(this Random random, ReadOnlyMemory<T> list)
Type parameters
T
Parameters
random Random
list ReadOnlyMemory<T>
Returns

T

NextFromList<T>(Random, ReadOnlySpan<T>)

RandomExtensions.cs

Returns a random element from a span.

public static T NextFromList<T>(this Random random, ReadOnlySpan<T> list)
Type parameters
T
Parameters
random Random
list ReadOnlySpan<T>
Returns

T

NextFromList<T>(Random, T[])

RandomExtensions.cs

Returns a random element from an array.

public static T NextFromList<T>(this Random random, T[] array)
Type parameters
T
Parameters
random Random
array T[]
Returns

T

NextInt16 overloads

NextInt16(Random)

RandomExtensions.cs

Returns a random 16-bit signed integer, from Int16.MinValue to Int16.MaxValue, both included.

public static short NextInt16(this Random random)
Parameters
random Random
Returns

short

NextInt16(Random, short, short)

RandomExtensions.cs

Returns a random 16-bit signed integer greater than or equal to min and less than max.

public static short NextInt16(this Random random, short min, short max)
Parameters
random Random
min short
max short
Returns

short

NextInt32(Random, int, int)

RandomExtensions.cs

Returns a random 32-bit signed integer within the specified range.

public static int NextInt32(this Random random, int min = 0, int max = 2147483647)

Parameters

random Random
min int
max int

Returns

int

NextInt64(Random, long, long)

RandomExtensions.cs
  • Obsolete: Use Random.NextInt64. The instance methods of Random take precedence over this extension method.

Returns a random 64-bit signed integer greater than or equal to min and less than max.

[Obsolete("Use Random.NextInt64. The instance methods of Random take precedence over this extension method.")]
public static long NextInt64(this Random random, long min = 0L, long max = 9223372036854775807L)

Parameters

random Random
min long
max long

Returns

long

Remarks

Random.NextInt64, Random.NextInt64 and Random.NextInt64 are instance methods, so random.NextInt64(...) never calls this extension method. This method calls Random.NextInt64.

NextSByte overloads

NextSByte(Random)

RandomExtensions.cs

Returns a random signed byte value, from SByte.MinValue to SByte.MaxValue, both included.

public static sbyte NextSByte(this Random random)
Parameters
random Random
Returns

sbyte

NextSByte(Random, sbyte, sbyte)

RandomExtensions.cs

Returns a random signed byte value greater than or equal to min and less than max.

public static sbyte NextSByte(this Random random, sbyte min, sbyte max)
Parameters
random Random
min sbyte
max sbyte
Returns

sbyte

NextSingle(Random, float, float)

RandomExtensions.cs

Returns a random single-precision floating point value greater than or equal to min and less than max.

public static float NextSingle(this Random random, float min = 0f, float max = 1f)

Parameters

random Random
min float
max float

Returns

float

Remarks

Random.NextSingle is an instance method, so random.NextSingle() calls it instead of this extension method. It returns the same range.

NextString overloads

NextString(Random, int, int, string)

RandomExtensions.cs

Returns a random string with length between the specified range using characters from the provided character set.

public static string NextString(this Random random, int minLength, int maxLength, string chars)
Parameters
random Random
minLength int
maxLength int
chars string
Returns

string

NextString(Random, int, string)

RandomExtensions.cs

Returns a random string of the specified length using characters from the provided character set.

public static string NextString(this Random random, int length, string chars)
Parameters
random Random
length int
chars string
Returns

string

NextUInt16 overloads

NextUInt16(Random)

RandomExtensions.cs

Returns a random 16-bit unsigned integer, from UInt16.MinValue to UInt16.MaxValue, both included.

public static ushort NextUInt16(this Random random)
Parameters
random Random
Returns

ushort

NextUInt16(Random, ushort, ushort)

RandomExtensions.cs

Returns a random 16-bit unsigned integer greater than or equal to min and less than max.

public static ushort NextUInt16(this Random random, ushort min, ushort max)
Parameters
random Random
min ushort
max ushort
Returns

ushort

NextUInt32 overloads

NextUInt32(Random)

RandomExtensions.cs

Returns a random 32-bit unsigned integer, from UInt32.MinValue to UInt32.MaxValue, both included.

public static uint NextUInt32(this Random random)
Parameters
random Random
Returns

uint

NextUInt32(Random, uint, uint)

RandomExtensions.cs

Returns a random 32-bit unsigned integer greater than or equal to min and less than max.

public static uint NextUInt32(this Random random, uint min, uint max)
Parameters
random Random
min uint
max uint
Returns

uint

NextUInt64 overloads

NextUInt64(Random)

RandomExtensions.cs

Returns a random 64-bit unsigned integer, from UInt64.MinValue to UInt64.MaxValue, both included.

public static ulong NextUInt64(this Random random)
Parameters
random Random
Returns

ulong

NextUInt64(Random, ulong, ulong)

RandomExtensions.cs

Returns a random 64-bit unsigned integer greater than or equal to min and less than max.

public static ulong NextUInt64(this Random random, ulong min, ulong max)
Parameters
random Random
min ulong
max ulong
Returns

ulong