Extension methods for System.Random class
$ dotnet add package Cayd.Random.ExtensionsThis package extends the System.Random class for more random generators, such as generating random values for all numeric types with or without a range (sbyte, byte, short, ushort, int, uint, long, ulong, float, double, decimal) and generating a boolean value with or without a probability.
After installing the package and adding Cayd.Random.Extensions namespace to your code, you can start using the extensions method of the package.
A few example of how to use the library:
using Cayd.Random.Extensions
Random rnd = new Random();
bool rndBool = rnd.NextBool(0.7);
decimal rndDecimal rnd.NextDecimal(100m, 1000000000m);
uint rndUint = rnd.NextUInt(100u, uint.MaxValue);
ulong rndUlong = rnd.NextULong(5000000uL);
// or for .Net 6 or higher, the static 'Shared' property can be used
bool rndBool = Random.Shared.NextBool(0.7);
decimal rndDecimal Random.Shared.NextDecimal(100m, 1000000000m);
uint rndUint = Random.Shared.NextUInt(100u, uint.MaxValue);
ulong rndUlong = Random.Shared.NextULong(5000000uL);
| Type | (No Parameter) | Only Max | Min and Max |
|---|---|---|---|
sbyte | NextSByte() | NextSByte(maxValue) | NextSByte(minValue, maxValue) |
byte | NextByte() | NextByte(maxValue) | NextByte(minValue, maxValue) |
short | NextShort() | NextShort(maxValue) | NextShort(minValue, maxValue) |
ushort | NextUShort() | NextUShort(maxValue) | NextUShort(minValue, maxValue) |
int | NextInt() | NextInt(maxValue) | NextInt(minValue, maxValue) |
uint | NextUInt() | NextUInt(maxValue) | NextUInt(minValue, maxValue) |
long | NextLong() | NextLong(maxValue) | NextLong(minValue, maxValue) |
ulong | NextULong() | NextULong(maxValue) | NextULong(minValue, maxValue) |
float | NextFloat() | NextFloat(maxValue) | NextFloat(minValue, maxValue) |
double | NextDoubleLimits() | NextDouble(maxValue) | NextDouble(minValue, maxValue) |
decimal | NextDecimal() | NextDecimal(maxValue) | NextDecimal(minValue, maxValue) |
For boolean:
true.true.| Type | (No Parameter) | Probability |
|---|---|---|
bool | NextBool() | NextBool(probability) |