Generating Random Numbers in C#
suggest changeSyntax
- Random()
- Random(int Seed)
- int Next()
- int Next(int maxValue)
- int Next(int minValue, int maxValue)
Parameters
| Parameter | Details |
|---|---|
| Seed | Value used to start the generator. If omitted, the default constructor uses the system clock. |
| minValue | Inclusive lower bound. Next(int, int) throws if minValue is greater than maxValue. |
| maxValue | Exclusive upper bound for Next(int) and Next(int, int). Next(0) returns 0. A negative maxValue throws ArgumentOutOfRangeException. |
| return value | The generated int. Parameterless Next() is in the range 0 inclusive to int.MaxValue exclusive. |
Remarks
Two Random instances created in the same clock tick can share that default seed and then produce the same sequence. Pass distinct seeds, or reuse one instance, when the sequences must differ. The same seed is repeatable on a given implementation of Random; the algorithm is not guaranteed to stay the same across major .NET versions.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents