3using System.Collections.Generic;
5using System.Runtime.Serialization;
17 [DataContract(Namespace = Constants.DataContractNamespace)]
28 [DataMember(Order = 1)]
29 public int Seed {
get;
private set; }
34 [DataMember(Order = 2)]
40 [DataMember(Order = 3)]
46 [DataMember(Order = 4)]
47 private int[]
Seeds {
get;
set; } =
new int[56];
82 return $
"RandomSeed(Seed = {Seed})";
97 private static int Mod(
int value)
100 return value +
int.MaxValue;
124 var mj = seed ==
int.MinValue ?
int.MaxValue : Math.Abs(seed);
130 for (
int i = 1; i < 55; i++)
132 var index = 21 * i % 55;
139 for (
int k = 1; k < 5; k++)
141 for (
int i = 1; i < 56; i++)
158 if (next ==
int.MaxValue)
181 public int Next(
int minValue,
int maxValue)
183 var delta = maxValue - (long)minValue;
185 return (
int)((long)(t * delta) + minValue);
220 return Next() / (double)
int.MaxValue;
240 return x * maxValue + (1 -
x) * minValue;
246 public (
float x,
float y) FloatInsideUnitCircle()
248 var (
x, y) = DoubleInsideUnitCircle();
249 return ((
float)
x, (
float)y);
255 public (
double x,
double y) DoubleInsideUnitCircle()
259 var
x = radius * Math.Cos(angle);
260 var y = radius * Math.Sin(angle);
274 double result = next;
275 result +=
int.MaxValue - 1;
276 result /= 2.0 *
int.MaxValue - 1;
286 for (
int i = 0; i < list.Count - 1; i++)
288 var j =
Next(i, list.Count);
289 (list[i], list[j]) = (list[j], list[i]);
299 var copy =
new List<T>(collection);
310 if (totals.Count > 0)
312 var value =
NextDouble(totals[totals.Count - 1]);
314 for (
int i = 0; i < totals.Count; i++)
316 if (value <= totals[i] && totals[i] > 0)
330 if (weights.Count > 0)
332 var totals =
new List<double>(weights.Count);
345 if (weights.Count > 0)
347 var totals =
new List<double>(weights.Count);
401 var result =
new List<int>(count);
403 if (weights.Count > 0)
407 for (
int i = 0; i < count; i++)
411 if ((uint)index < (uint)weights.Count)
426 count = Math.Min(count, weights.Count);
427 var result =
new List<int>(count);
429 if (weights.Count > 0)
431 var weightsCopy = weights.ToArray();
432 var totals =
new List<double>(weightsCopy.Length);
434 for (
int i = 0; i < count; i++)
438 if ((uint)index < (uint)weightsCopy.Length)
440 weightsCopy[index] = 0;
472 var result =
new List<int>(count);
474 if (weights.Count > 0)
478 for (
int i = 0; i < count; i++)
482 if ((uint)index < (uint)weights.Count)
497 count = Math.Min(count, weights.Count);
498 var result =
new List<int>(count);
500 if (weights.Count > 0)
502 var weightsCopy = weights.ToArray();
503 var totals =
new List<double>(weightsCopy.Length);
505 for (
int i = 0; i < count; i++)
509 if ((uint)index < (uint)weightsCopy.Length)
511 weightsCopy[index] = 0;
526 return (
NextDouble() <= chance && chance > 0) || chance >= 1;
Contains extra math operations.
static double[] CumSum(IList< double > values)
Returns the cumulative sums of the list.
A class for performing pseudo-random number generation.
List< int > DrawWeightedIndexesWithReplacement(IList< float > weights, int count)
Draws a quantity of random weighted indexes from a list with replacement.
int DrawIndex(IList< double > totals)
Draws an index from the list of cumulative weights.
int Next(int minValue, int maxValue)
Returns a random value on the interval [minValue, maxValue).
RandomSeed(RandomSeed other)
Initializes a copy of a random seed.
List< T > Shuffled< T >(IEnumerable< T > collection)
Returns a new shuffled copy of the collection.
double NextDouble(double maxValue)
Returns a random double on the interval [0, maxValue).
RandomSeed(int seed)
Initializes a new random seed.
double NextDouble()
Returns a random double on the interval [0, 1).
double NextDouble(double minValue, double maxValue)
Returns a random double on the interval [minValue, maxValue).
static int Mod(int value)
Returns the positive modulo of a value with respect to int.MaxValue.
int[] Seeds
An array of previous seeds.
int DrawWeightedIndex(IList< float > weights)
Draws a random weighted index from a list.
float NextFloat(float minValue, float maxValue)
Returns a random float on the interval [minValue, maxValue).
void SetSeed(int seed)
Sets the random seed and initializes the randomizer.
int DrawWeightedIndex(IList< float > weights, List< double > totals)
Draws a random weighted index from a list. The specified totals buffer allows for minimal garbage gen...
List< int > DrawWeightedIndexesWithoutReplacement(IList< double > weights, int count)
Draws a quantity of random weighted indexes from a list without replacement.
int Next(int maxValue)
Returns a random value on the interval [0, maxValue).
bool ChanceSatisfied(double chance)
Draws a random number and returns true if it satisfies the specified probability.
int Position2
The second position of the randomizer.
override string ToString()
int Next()
Returns a random integer on the interval [0, int.MaxValue).
float NextFloat()
Returns a random float on the interval [0, 1).
List< int > DrawWeightedIndexesWithoutReplacement(IList< float > weights, int count)
Draws a quantity of random weighted indexes from a list without replacement.
List< int > DrawWeightedIndexes(IList< float > weights, int count, bool withReplacement)
Draws a quantity of random weighted indexes from a list. Based on the draw weights and whether indexe...
static int WrapIndex(int value)
Wraps an index if it exceeds the top array bounds.
float x
Returns a random floating precision point inside the unit circle.
RandomSeed Copy()
Returns a copy of the object.
List< int > DrawWeightedIndexesWithReplacement(IList< double > weights, int count)
Draws a quantity of random weighted indexes from a list with replacement.
static RandomSeed Current
The current global random number generator.
int Position1
The first position of the randomizer.
RandomSeed()
Initializes a new random seed based on the current system ticks.
List< int > DrawWeightedIndexes(IList< double > weights, int count, bool withReplacement)
Draws a quantity of random weighted indexes from a list. Based on the draw weights and whether indexe...
int DrawWeightedIndex(IList< double > weights, List< double > totals)
Draws a random weighted index from a list. The specified totals buffer allows for minimal garbage gen...
void Shuffle< T >(IList< T > list)
Shuffles the specified list in place.
int DrawWeightedIndex(IList< double > weights)
Draws a random weighted index from a list.
double x
Returns a random double precision point inside the unit circle.
float NextFloat(float maxValue)
Returns a random float on the interval [0, maxValue).
double NextLargeDouble()
Returns a random high resolution double on the interval [0, 1).