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
minValueshortminimum value to generate (inclusive).
maxValueshortmaximum value to generate (exclusive).
Returns
- short
The random
shortin range[minValue, maxValue).
Remarks
minValue must be less than or equal to maxValue.
Exceptions
- ArgumentOutOfRangeException
If
minValueis greater thanmaxValue.
Next(int, int)
Generates a random int.
public static int Next(int minValue, int maxValue)
Parameters
minValueintminimum value to generate (inclusive).
maxValueintmaximum value to generate (exclusive).
Returns
- int
The random
intin range[minValue, maxValue).
Remarks
minValue must be less than or equal to maxValue.
Exceptions
- ArgumentOutOfRangeException
If
minValueis greater thanmaxValue.
Next(long, long)
Generates a random long.
public static long Next(long minValue, long maxValue)
Parameters
minValuelongminimum value to generate (inclusive).
maxValuelongmaximum value to generate (exclusive).
Returns
- long
The random
longin range[minValue, maxValue).
Remarks
minValue must be less than or equal to maxValue.
Exceptions
- ArgumentOutOfRangeException
If
minValueis greater thanmaxValue.
Next(string)
Generates a random char from the given set of characters.
public static char Next(string chars)
Parameters
charsstringThe set of allowed characters to generate from.
Returns
Remarks
If chars is null or empty, '\0' is returned.