Show / Hide Table of Contents

Class RandomExtensions

Provides random data generation.

Inheritance
object
RandomExtensions
Inherited Members
object.Equals(object)
object.Equals(object, object)
object.GetHashCode()
object.GetType()
object.MemberwiseClone()
object.ReferenceEquals(object, object)
object.ToString()
Namespace: DotNext
Assembly: DotNext.dll
Syntax
public static class RandomExtensions

Methods

| Edit this page View Source

GetItems<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

random is null.

| Edit this page View Source

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

random is null.

| Edit this page View Source

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).

| Edit this page View Source

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

trueProbability value is invalid.

| Edit this page View Source

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

trueProbability value is invalid.

| Edit this page View Source

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

random is null.

| Edit this page View Source

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

random is null.

| Edit this page View Source

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.

| Edit this page View Source

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

random is null.

ArgumentOutOfRangeException

length is less than zero.

| Edit this page View Source

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

random is null.

ArgumentOutOfRangeException

length is less than zero.

| Edit this page View Source

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

random is null.

ArgumentOutOfRangeException

length is less than zero.

| Edit this page View Source

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

random is null.

ArgumentOutOfRangeException

length is less than zero.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

| Edit this page View Source

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.

  • Edit this page
  • View Source
☀
☾
In this article
Back to top
Supported by the .NET Foundation
☀
☾