Table of Contents

Class RandomExtensions

Namespace
RandomizerCore.Extensions
Assembly
RandomizerCore.dll
public static class RandomExtensions
Inheritance
RandomExtensions
Inherited Members

Methods

ClampToMultipleOf(double, int)

Rounds toward 0 to the largest integer multiple of the divisor which is less than the double in magnitude.

public static int ClampToMultipleOf(this double d, int divisor)

Parameters

d double
divisor int

Returns

int

NextBool(Random)

Returns a random bool.

public static bool NextBool(this Random rng)

Parameters

rng Random

Returns

bool

NextWhere<T>(Random, IReadOnlyList<T>, Predicate<T>)

Creates a list of elements which satisfy the predicate, and returns one of the elements of the list uniformly at random.

public static T NextWhere<T>(this Random rng, IReadOnlyList<T> ts, Predicate<T> test)

Parameters

rng Random
ts IReadOnlyList<T>
test Predicate<T>

Returns

T

Type Parameters

T

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

Selects an element of the list uniformly at random.

public static T Next<T>(this Random rand, IReadOnlyList<T> ts)

Parameters

rand Random
ts IReadOnlyList<T>

Returns

T

Type Parameters

T

Next<T>(Random, IReadOnlyList<T>, IEnumerable<double>, bool)

Selects an element of the list randomly according to the provided weights.
If cumulative, weights should be an increasing sequence of positive numbers with last entry 1. Probability of selecting element i for i>0 is weights[i] - weights[i-1], and weights[0] for i=0.
If noncumulative, weights should be a sequence of nonnegative numbers. Probability of selecting element i is weights[i] / total, where total is the sum of the weights.

public static T Next<T>(this Random rand, IReadOnlyList<T> ts, IEnumerable<double> weights, bool cumulativeWeights = false)

Parameters

rand Random
ts IReadOnlyList<T>
weights IEnumerable<double>
cumulativeWeights bool

Returns

T

Type Parameters

T

Permute(Random, int)

Returns a new array containing a random permutation of the integers from 0 to n-1.

public static int[] Permute(this Random rand, int n)

Parameters

rand Random
n int

Returns

int[]

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

Randomly permutes the input list.

public static void PermuteInPlace<T>(this Random rand, IList<T> input)

Parameters

rand Random
input IList<T>

Type Parameters

T

PermuteInPlace<T>(Random, IList<T>, Action<T, int>)

Randomly permutes the input list, and performs an action on each element and its new index.

public static void PermuteInPlace<T>(this Random rand, IList<T> input, Action<T, int> action)

Parameters

rand Random
input IList<T>
action Action<T, int>

Type Parameters

T

PermuteInPlace<T>(Random, T[])

Randomly permutes the input array.

public static void PermuteInPlace<T>(this Random rand, T[] input)

Parameters

rand Random
input T[]

Type Parameters

T

Permute<T>(Random, IEnumerable<T>)

Returns a list containing a random permutation of the output of the enumerable.

public static List<T> Permute<T>(this Random rand, IEnumerable<T> input)

Parameters

rand Random
input IEnumerable<T>

Returns

List<T>

Type Parameters

T

Permute<T>(Random, T[])

Returns a new array containing a random permutation of the elements of the input.

public static T[] Permute<T>(this Random rand, T[] input)

Parameters

rand Random
input T[]

Returns

T[]

Type Parameters

T

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

Removes and returns an element of the list, selected uniformly at random.

public static T PopNext<T>(this Random rand, IList<T> ts)

Parameters

rand Random
ts IList<T>

Returns

T

Type Parameters

T

PopNext<T>(Random, IList<T>, IEnumerable<double>, bool)

Removes and returns an element of the list, selected randomly according to the provided weights.
If cumulative, weights should be an increasing sequence of positive numbers with last entry 1. Probability of selecting element i for i>0 is weights[i] - weights[i-1], and weights[0] for i=0.
If noncumulative, weights should be a sequence of nonnegative numbers. Probability of selecting element i is weights[i] / total, where total is the sum of the weights.

public static T PopNext<T>(this Random rand, IList<T> ts, IEnumerable<double> weights, bool cumulativeWeights = false)

Parameters

rand Random
ts IList<T>
weights IEnumerable<double>
cumulativeWeights bool

Returns

T

Type Parameters

T

Pop<T>(IList<T>)

Removes and returns the last element of the list.

public static T Pop<T>(this IList<T> list)

Parameters

list IList<T>

Returns

T

Type Parameters

T

Pop<T>(IList<T>, int)

Removes and returns the element of the list at the requested index.

public static T Pop<T>(this IList<T> list, int index = 0)

Parameters

list IList<T>
index int

Returns

T

Type Parameters

T

Pop<T>(IList<T>, Predicate<T>)

Removes and returns the first element of the list which satisfies the predicate.

public static T Pop<T>(this IList<T> list, Predicate<T> TSelector)

Parameters

list IList<T>
TSelector Predicate<T>

Returns

T

Type Parameters

T

PowerLaw(Random, double, double, double)

Returns a random double within the requested interval, according to a power distribution.
Equivalent to scaling the interval by the reciprocal power, selecting a double within that interval uniformly, and rescaling the result by the power.

public static double PowerLaw(this Random rng, double pow, double min, double max)

Parameters

rng Random
pow double
min double
max double

Returns

double

Slice<T>(IReadOnlyList<T>, int, int)

Returns the segment of the list starting at the specified index, with the specified count.

public static IEnumerable<T> Slice<T>(this IReadOnlyList<T> list, int start, int count)

Parameters

list IReadOnlyList<T>
start int
count int

Returns

IEnumerable<T>

Type Parameters

T

Slice<T>(T[], int, int)

Returns the segment of the array starting at the specified index, with the specified count.

public static IEnumerable<T> Slice<T>(this T[] list, int start, int count)

Parameters

list T[]
start int
count int

Returns

IEnumerable<T>

Type Parameters

T

Swap<T>(T[], int, int)

Swaps array elements at two indices.

public static void Swap<T>(this T[] arr, int i, int j)

Parameters

arr T[]
i int
j int

Type Parameters

T

TryPop<T>(IList<T>, Predicate<T>, out T)

Searches for the first element of the list which satisfies the predicate. If found, returns true, and removes and outputs that element. Otherwise, returns false.

public static bool TryPop<T>(this IList<T> list, Predicate<T> TSelector, out T val)

Parameters

list IList<T>
TSelector Predicate<T>
val T

Returns

bool

Type Parameters

T