Random
Source: lib/Random.cs
This page is generated from the source by scripts/gen-reference.py.
Random numbers: xoshiro256** (Blackman and Vigna), seeded through SplitMix64.
var random = new Random(42); // the same numbers on every target
int die = random.Next(1, 7); // 1 to 6
double x = random.NextDouble(); // 0 <= x < 1
random.Shuffle(cards);
var varied = Random.FromClock(); // different numbers each run
The members have .NET's names, but the generator is a struct holding its state: a copy goes on from the same point and repeats the original's numbers. Pass one by ref. default(Random) is new Random(0). The numbers are fast and statistically good, but predictable: not for keys, tokens or passwords.
A random number generator (xoshiro256**). A struct: pass it by ref.
Members
Random
public Random(ulong seed)
A generator whose numbers are fixed by seed, on every target.
FromClock
public static Random FromClock()
A generator seeded from the clocks, so each run gets different numbers.
NextUInt64
public ulong NextUInt64()
64 random bits.
Next
public int Next()
A number from 0 up to but not including int.MaxValue.
public int Next(int maxValue)
A number from 0 up to but not including maxValue; 0 if it is 0.
public int Next(int minValue, int maxValue)
A number from minValue up to but not including maxValue; minValue if they are equal. Every number in the range is equally likely. Ends the program if maxValue is less than minValue.
NextInt64
public long NextInt64(long minValue, long maxValue)
A number from minValue up to but not including maxValue; minValue if they are equal. Ends the program if maxValue is less than minValue.
NextDouble
public double NextDouble()
A number from 0 up to but not including 1, in steps of 2^-53.
NextSingle
public float NextSingle()
A number from 0 up to but not including 1, in steps of 2^-24.
NextBytes
public void NextBytes(Span<byte> buffer)
Fills buffer with random bytes.
Shuffle
public void Shuffle<T>(Span<T> values)
Puts values in a random order, each order equally likely.