Class RandomGen
Provides methods for generating random values of various types.
public static class RandomGen
- Inheritance
-
RandomGen
- Inherited Members
Methods
GetBool()
Returns a random boolean value.
public static bool GetBool()
Returns
- bool
A random boolean.
GetBytes(byte[])
Fills the provided array with random bytes.
public static void GetBytes(byte[] buffer)
Parameters
bufferbyte[]The array to fill with random bytes.
GetBytes(int)
Returns an array of random bytes with the specified count.
public static byte[] GetBytes(int count)
Parameters
countintThe number of random bytes to generate.
Returns
- byte[]
An array of random bytes.
GetDate()
Returns a random DateTime value between DateTime.MinValue and DateTime.MaxValue.
public static DateTime GetDate()
Returns
- DateTime
A random DateTime.
GetDate(DateTime, DateTime)
Returns a random DateTime value between the specified minimum and maximum values.
public static DateTime GetDate(DateTime min, DateTime max)
Parameters
Returns
- DateTime
A random DateTime between min and max.
GetDecimal(decimal, decimal, int)
Returns a random decimal value between the specified minimum and maximum values with the given precision.
public static decimal GetDecimal(decimal min, decimal max, int precision)
Parameters
mindecimalThe minimum decimal value.
maxdecimalThe maximum decimal value.
precisionintThe number of decimal places to round the value to.
Returns
- decimal
A random decimal value between min and max rounded to the specified precision.
GetDecimal(int, int)
Returns a random non-zero decimal value with a specified number of integer and fractional digits. Tries up to 10 times to generate a valid number.
public static decimal GetDecimal(int integer = 8, int fractional = 8)
Parameters
integerintThe maximum number of digits in the integer part. Default is 8.
fractionalintThe maximum number of digits in the fractional part. Default is 8.
Returns
- decimal
A random decimal value.
Exceptions
- InvalidOperationException
Thrown when a valid decimal value cannot be generated in 10 attempts.
GetDouble()
Returns a random double value between 0.0 and 1.0.
public static double GetDouble()
Returns
- double
A random double.
GetElement<T>(IEnumerable<T>)
Returns a random element from the specified collection.
public static T GetElement<T>(IEnumerable<T> array)
Parameters
arrayIEnumerable<T>The collection of elements.
Returns
- T
A random element from the collection.
Type Parameters
TThe type of elements in the collection.
GetEnum<T>()
Returns a random enum value of type T.
public static T GetEnum<T>() where T : struct
Returns
- T
A random enum value.
Type Parameters
TThe enum type.
GetEnum<T>(IEnumerable<T>)
Returns a random enum value from the specified collection of values.
public static T GetEnum<T>(IEnumerable<T> values) where T : struct
Parameters
valuesIEnumerable<T>A collection of enum values.
Returns
- T
A random enum value from the collection.
Type Parameters
TThe enum type.
GetEnum<T>(T, T)
Returns a random enum value between the specified minimum and maximum enum values.
public static T GetEnum<T>(T min, T max) where T : struct
Parameters
minTThe minimum enum value.
maxTThe maximum enum value.
Returns
- T
A random enum value between min and max.
Type Parameters
TThe enum type.
GetInt()
Returns a random non-negative integer.
public static int GetInt()
Returns
- int
A random integer.
GetInt(int)
Returns a random integer between 0 and the specified maximum value (inclusive).
public static int GetInt(int max)
Parameters
maxintThe maximum value.
Returns
- int
A random integer between 0 and max (inclusive).
GetInt(int, int)
Returns a random integer between the specified minimum and maximum values (inclusive).
public static int GetInt(int min, int max)
Parameters
Returns
- int
A random integer between min and max (inclusive).
GetLong()
Returns a random long value between long.MinValue and long.MaxValue.
public static long GetLong()
Returns
- long
A random long value.
GetLong(long, long)
Returns a random long value between the specified minimum and maximum values (inclusive).
public static long GetLong(long min, long max)
Parameters
Returns
- long
A random long value between min and max (inclusive).
GetString(int, int)
Returns a random Base64 encoded string generated from a random salt.
public static string GetString(int min, int max)
Parameters
minintThe minimum length for generating the salt.
maxintThe maximum length for generating the salt.
Returns
- string
A random Base64 encoded string.
GetTime()
Returns a random TimeSpan value between TimeSpan.MinValue and TimeSpan.MaxValue.
public static TimeSpan GetTime()
Returns
- TimeSpan
A random TimeSpan.
GetTime(TimeSpan, TimeSpan)
Returns a random TimeSpan value between the specified minimum and maximum values.
public static TimeSpan GetTime(TimeSpan min, TimeSpan max)
Parameters
Returns
- TimeSpan
A random TimeSpan between min and max.