Class RandomExtensions
Provides random data generation.
Inherited Members
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public static class RandomExtensions
Methods
| Edit this page View SourceGetItems<T>(Random, Span<T>)
Fills the buffer with random values of the specified type.
Declaration
public static void GetItems<T>(this Random random, Span<T> buffer) where T : unmanaged
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random numbers. |
Span<T> | buffer | The buffer to fill with random values. |
Type Parameters
Name | Description |
---|---|
T | The blittable type. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
GetItems<T>(RandomNumberGenerator, Span<T>)
Fills the buffer with random values of the specified type.
Declaration
public static void GetItems<T>(this RandomNumberGenerator random, Span<T> buffer) where T : unmanaged
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
Span<T> | buffer | The buffer to fill with random values. |
Type Parameters
Name | Description |
---|---|
T | The blittable type. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
Next(RandomNumberGenerator)
Generates non-negative integer.
Declaration
public static int Next(this RandomNumberGenerator random)
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
Returns
Type | Description |
---|---|
int | A 32-bit signed integer that is in range [0, MaxValue). |
NextBoolean(Random, double)
Generates a random boolean value.
Declaration
public static bool NextBoolean(this Random random, double trueProbability = 0.5)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random numbers. |
double | trueProbability | A probability of true result (should be between 0.0 and 1.0). |
Returns
Type | Description |
---|---|
bool | Randomly generated boolean value. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
NextBoolean(RandomNumberGenerator, double)
Generates a random boolean value.
Declaration
public static bool NextBoolean(this RandomNumberGenerator random, double trueProbability = 0.5)
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
double | trueProbability | A probability of true result (should be between 0.0 and 1.0). |
Returns
Type | Description |
---|---|
bool | Randomly generated boolean value. |
Exceptions
Type | Condition |
---|---|
ArgumentOutOfRangeException |
|
NextChars(Random, ReadOnlySpan<char>, Span<char>)
Generates a random set of characters.
Declaration
public static void NextChars(this Random random, ReadOnlySpan<char> allowedChars, Span<char> buffer)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random numbers. |
ReadOnlySpan<char> | allowedChars | The allowed characters for the random string. |
Span<char> | buffer | The array to be filled with random characters. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
NextChars(RandomNumberGenerator, ReadOnlySpan<char>, Span<char>)
Generates a random set of characters.
Declaration
public static void NextChars(this RandomNumberGenerator random, ReadOnlySpan<char> allowedChars, Span<char> buffer)
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
ReadOnlySpan<char> | allowedChars | The allowed characters for the random string. |
Span<char> | buffer | The array to be filled with random characters. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
NextDouble(RandomNumberGenerator)
Returns a random floating-point number that is in range [0, 1).
Declaration
public static double NextDouble(this RandomNumberGenerator random)
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
Returns
Type | Description |
---|---|
double | Randomly generated floating-point number. |
NextString(Random, ReadOnlySpan<char>, int)
Generates a random string of the given length.
Declaration
public static string NextString(this Random random, ReadOnlySpan<char> allowedChars, int length)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random numbers. |
ReadOnlySpan<char> | allowedChars | The allowed characters for the random string. |
int | length | The length of the random string. |
Returns
Type | Description |
---|---|
string | Randomly generated string. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
NextString(Random, string, int)
Generates a random string of the given length.
Declaration
public static string NextString(this Random random, string allowedChars, int length)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random numbers. |
string | allowedChars | The string of allowed characters for the random string. |
int | length | The length of the random string. |
Returns
Type | Description |
---|---|
string | Randomly generated string. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
NextString(RandomNumberGenerator, ReadOnlySpan<char>, int)
Generates a random string of the given length.
Declaration
public static string NextString(this RandomNumberGenerator random, ReadOnlySpan<char> allowedChars, int length)
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
ReadOnlySpan<char> | allowedChars | The allowed characters for the random string. |
int | length | The length of the random string. |
Returns
Type | Description |
---|---|
string | Randomly generated string. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
NextString(RandomNumberGenerator, string, int)
Generates a random string of the given length.
Declaration
public static string NextString(this RandomNumberGenerator random, string allowedChars, int length)
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
string | allowedChars | The string of allowed characters for the random string. |
int | length | The length of the random string. |
Returns
Type | Description |
---|---|
string | Randomly generated string. |
Exceptions
Type | Condition |
---|---|
ArgumentNullException |
|
ArgumentOutOfRangeException |
|
Next<T>(Random)
Generates a random value of blittable type.
Declaration
public static T Next<T>(this Random random) where T : unmanaged
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random numbers. |
Returns
Type | Description |
---|---|
T | The randomly generated value. |
Type Parameters
Name | Description |
---|---|
T | The blittable type. |
Next<T>(RandomNumberGenerator)
Generates a random value of blittable type.
Declaration
public static T Next<T>(this RandomNumberGenerator random) where T : unmanaged
Parameters
Type | Name | Description |
---|---|---|
RandomNumberGenerator | random | The source of random numbers. |
Returns
Type | Description |
---|---|
T | The randomly generated value. |
Type Parameters
Name | Description |
---|---|
T | The blittable type. |
Peek<T>(Random, IReadOnlyCollection<T>)
Gets the random element from the collection.
Declaration
public static Optional<T> Peek<T>(this Random random, IReadOnlyCollection<T> collection)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The random numbers source. |
IReadOnlyCollection<T> | collection | The collection to get the random element. |
Returns
Type | Description |
---|---|
Optional<T> | The random element from the collection; or None if collection is empty. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the collection. |
Peek<T>(Random, ReadOnlySpan<T>)
Chooses the random element in the span.
Declaration
public static Optional<T> Peek<T>(this Random random, ReadOnlySpan<T> span)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random values. |
ReadOnlySpan<T> | span | The span of elements. |
Returns
Type | Description |
---|---|
Optional<T> | Randomly selected element from the span; or None if span is empty. |
Type Parameters
Name | Description |
---|---|
T | The type of elements in the span. |
Shuffle<T>(Random, IList<T>)
Randomizes elements in the list.
Declaration
public static void Shuffle<T>(this Random random, IList<T> list)
Parameters
Type | Name | Description |
---|---|---|
Random | random | The source of random values. |
IList<T> | list | The list to shuffle. |
Type Parameters
Name | Description |
---|---|
T | The type of items in the list. |