Class SingleRandom
The SingleRandom class allows generating single, one-off pseudorandom numbers based on a seed or a SeedSource. The types of numbers that can be generated are int and float, both of which can be generated with specific minimum and maximum values if desired. Methods in this class are tested to be sufficiently "random", that is, to be sufficiently distributed throughout their range, as well as sufficiently different for neighboring seeds.
public class SingleRandom
- Inheritance
-
SingleRandom
- Inherited Members
Methods
GetRandomEntry<T>(ICollection<T>, SeedSource)
Gets a random entry from the given collection with uniform chance.
public static T GetRandomEntry<T>(ICollection<T> entries, SeedSource source)
Parameters
entries
ICollection<T>The entries to choose from
source
SeedSourceThe SeedSource to use.
Returns
- T
A random entry
Type Parameters
T
The entries' type
GetRandomWeightedEntry<T>(ICollection<T>, Func<T, int>, SeedSource)
Returns a random entry from the given collection based on the specified weight function. A higher weight for an entry increases its likeliness of being picked.
public static T GetRandomWeightedEntry<T>(ICollection<T> entries, Func<T, int> weightFunc, SeedSource source)
Parameters
entries
ICollection<T>The entries to choose from
weightFunc
Func<T, int>A function that applies weight to each entry
source
SeedSourceThe SeedSource to use.
Returns
- T
A random entry, based on the entries' weight
Type Parameters
T
The entries' type
Exceptions
- IndexOutOfRangeException
If the weight function returns different weights for the same entry
GetRandomWeightedEntry<T>(ICollection<T>, Func<T, float>, SeedSource)
Returns a random entry from the given collection based on the specified weight function. A higher weight for an entry increases its likeliness of being picked.
public static T GetRandomWeightedEntry<T>(ICollection<T> entries, Func<T, float> weightFunc, SeedSource source)
Parameters
entries
ICollection<T>The entries to choose from
weightFunc
Func<T, float>A function that applies weight to each entry
source
SeedSourceThe SeedSource to use.
Returns
- T
A random entry, based on the entries' weight
Type Parameters
T
The entries' type
Exceptions
- IndexOutOfRangeException
If the weight function returns different weights for the same entry
Int(SeedSource)
Generates a single, one-off pseudorandom integer between 0 and MaxValue based on a given source
.
This method is guaranteed to return the same result for the same source
.
public static int Int(SeedSource source)
Parameters
source
SeedSourceThe SeedSource to use.
Returns
- int
The generated number.
Int(int)
Generates a single, one-off pseudorandom integer between 0 and MaxValue based on a given seed
.
This method is guaranteed to return the same result for the same seed
.
public static int Int(int seed)
Parameters
seed
intThe seed to use.
Returns
- int
The generated number.
Int(int, SeedSource)
Generates a single, one-off pseudorandom integer between 0 and maxValue
based on a given source
.
This method is guaranteed to return the same result for the same source
.
public static int Int(int maxValue, SeedSource source)
Parameters
maxValue
intThe (exclusive) maximum value.
source
SeedSourceThe SeedSource to use.
Returns
- int
The generated number.
Int(int, int)
Generates a single, one-off pseudorandom integer between 0 and maxValue
based on a given seed
.
This method is guaranteed to return the same result for the same seed
.
public static int Int(int maxValue, int seed)
Parameters
Returns
- int
The generated number.
Int(int, int, SeedSource)
Generates a single, one-off pseudorandom integer between minValue
and maxValue
based on a given source
.
This method is guaranteed to return the same result for the same source
.
public static int Int(int minValue, int maxValue, SeedSource source)
Parameters
minValue
intThe (inclusive) minimum value.
maxValue
intThe (exclusive) maximum value.
source
SeedSourceThe SeedSource to use.
Returns
- int
The generated number.
Int(int, int, int)
Generates a single, one-off pseudorandom integer between minValue
and maxValue
based on a given seed
.
This method is guaranteed to return the same result for the same seed
.
public static int Int(int minValue, int maxValue, int seed)
Parameters
minValue
intThe (inclusive) minimum value.
maxValue
intThe (exclusive) maximum value.
seed
intThe seed to use.
Returns
- int
The generated number.
Single(SeedSource)
Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given source
.
This method is guaranteed to return the same result for the same source
.
public static float Single(SeedSource source)
Parameters
source
SeedSourceThe SeedSource to use.
Returns
- float
The generated number.
Single(int)
Generates a single, one-off pseudorandom floating point number between 0 and 1 based on a given seed
.
This method is guaranteed to return the same result for the same seed
.
public static float Single(int seed)
Parameters
seed
intThe seed to use.
Returns
- float
The generated number.
Single(float, SeedSource)
Generates a single, one-off pseudorandom floating point number between 0 and maxValue
based on a given source
.
This method is guaranteed to return the same result for the same source
.
public static float Single(float maxValue, SeedSource source)
Parameters
maxValue
floatThe (exclusive) maximum value.
source
SeedSourceThe SeedSource to use.
Returns
- float
The generated number.
Single(float, int)
Generates a single, one-off pseudorandom floating point number between 0 and maxValue
based on a given seed
.
This method is guaranteed to return the same result for the same seed
.
public static float Single(float maxValue, int seed)
Parameters
Returns
- float
The generated number.
Single(float, float, SeedSource)
Generates a single, one-off pseudorandom floating point number between minValue
and maxValue
based on a given source
.
This method is guaranteed to return the same result for the same source
.
public static float Single(float minValue, float maxValue, SeedSource source)
Parameters
minValue
floatThe (inclusive) minimum value.
maxValue
floatThe (exclusive) maximum value.
source
SeedSourceThe SeedSource to use.
Returns
- float
The generated number.
Single(float, float, int)
Generates a single, one-off pseudorandom floating point number between minValue
and maxValue
based on a given seed
.
This method is guaranteed to return the same result for the same seed
.
public static float Single(float minValue, float maxValue, int seed)
Parameters
minValue
floatThe (inclusive) minimum value.
maxValue
floatThe (exclusive) maximum value.
seed
intThe seed to use.
Returns
- float
The generated number.