RandomExtensions Class
Provides extension methods for Random to generate various types of random values.
public static class RandomExtensionsMethods
| Name | Description |
|---|---|
| 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.csNextByte overloads
NextByte(Random)
RandomExtensions.csReturns a random byte value, from Byte.MinValue to Byte.MaxValue, both included.
NextByte(Random, byte, byte)
RandomExtensions.csNextDateTime(Random, DateTime, DateTime)
RandomExtensions.csNextDecimal(Random, decimal, decimal)
RandomExtensions.csReturns 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
Returns
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.csNextFromList overloads
NextFromList<T>(Random, ICollection<T>)
RandomExtensions.csReturns a random element from a collection.
public static T NextFromList<T>(this Random random, ICollection<T> list)Type parameters
T
Parameters
randomRandomlistICollection<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.csNextFromList<T>(Random, IReadOnlyCollection<T>)
RandomExtensions.csReturns a random element from a read-only collection.
public static T NextFromList<T>(this Random random, IReadOnlyCollection<T> list)Type parameters
T
Parameters
randomRandomlistIReadOnlyCollection<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.csReturns a random element from a read-only list.
public static T NextFromList<T>(this Random random, IReadOnlyList<T> list)NextFromList<T>(Random, ReadOnlyMemory<T>)
RandomExtensions.csReturns a random element from a memory.
public static T NextFromList<T>(this Random random, ReadOnlyMemory<T> list)NextFromList<T>(Random, ReadOnlySpan<T>)
RandomExtensions.csReturns a random element from a span.
public static T NextFromList<T>(this Random random, ReadOnlySpan<T> list)NextFromList<T>(Random, T[])
RandomExtensions.csNextInt16 overloads
NextInt16(Random)
RandomExtensions.csReturns a random 16-bit signed integer, from Int16.MinValue to Int16.MaxValue, both included.
NextInt16(Random, short, short)
RandomExtensions.csNextInt32(Random, int, int)
RandomExtensions.csNextInt64(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
Returns
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.csReturns a random signed byte value, from SByte.MinValue to SByte.MaxValue, both included.
NextSByte(Random, sbyte, sbyte)
RandomExtensions.csNextSingle(Random, float, float)
RandomExtensions.csReturns a random single-precision floating point value greater than or equal to min and less than max.
Parameters
Returns
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.csNextString(Random, int, string)
RandomExtensions.csNextUInt16 overloads
NextUInt16(Random)
RandomExtensions.csReturns a random 16-bit unsigned integer, from UInt16.MinValue to UInt16.MaxValue, both included.
NextUInt16(Random, ushort, ushort)
RandomExtensions.csNextUInt32 overloads
NextUInt32(Random)
RandomExtensions.csReturns a random 32-bit unsigned integer, from UInt32.MinValue to UInt32.MaxValue, both included.
NextUInt32(Random, uint, uint)
RandomExtensions.csNextUInt64 overloads
NextUInt64(Random)
RandomExtensions.csReturns a random 64-bit unsigned integer, from UInt64.MinValue to UInt64.MaxValue, both included.