Class RandomUtils
Utility class to generate random values using cryptographically strong value generator RandomNumberGenerator.
public static class RandomUtils
- Inheritance
-
RandomUtils
- Inherited Members
Examples
using Ddth.Utilities;
// Generate a random int in range [0, 100)
var randomInt = RandomUtils.Next(0, 100);
// Generate a random char from the set "abc"
var randomChar = RandomUtils.Next("abc");
Methods
Next(short, short)
Generates a random short
.
public static short Next(short minValue, short maxValue)
Parameters
minValue
shortminimum value to generate (inclusive).
maxValue
shortmaximum value to generate (exclusive).
Returns
- short
The random
short
in range[minValue, maxValue)
.
Remarks
minValue
must be less than or equal to maxValue
.
Exceptions
- ArgumentOutOfRangeException
If
minValue
is greater thanmaxValue
.
Next(int, int)
Generates a random int
.
public static int Next(int minValue, int maxValue)
Parameters
minValue
intminimum value to generate (inclusive).
maxValue
intmaximum value to generate (exclusive).
Returns
- int
The random
int
in range[minValue, maxValue)
.
Remarks
minValue
must be less than or equal to maxValue
.
Exceptions
- ArgumentOutOfRangeException
If
minValue
is greater thanmaxValue
.
Next(long, long)
Generates a random long
.
public static long Next(long minValue, long maxValue)
Parameters
minValue
longminimum value to generate (inclusive).
maxValue
longmaximum value to generate (exclusive).
Returns
- long
The random
long
in range[minValue, maxValue)
.
Remarks
minValue
must be less than or equal to maxValue
.
Exceptions
- ArgumentOutOfRangeException
If
minValue
is greater thanmaxValue
.
Next(string)
Generates a random char
from the given set of characters.
public static char Next(string chars)
Parameters
chars
stringThe set of allowed characters to generate from.
Returns
Remarks
If chars
is null or empty, '\0'
is returned.