Table of Contents

Class RandomGen

Namespace
Ecng.Common
Assembly
Ecng.Common.dll

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

buffer byte[]

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

count int

The 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

min DateTime

The minimum DateTime value.

max DateTime

The maximum DateTime value.

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

min decimal

The minimum decimal value.

max decimal

The maximum decimal value.

precision int

The 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

integer int

The maximum number of digits in the integer part. Default is 8.

fractional int

The 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

array IEnumerable<T>

The collection of elements.

Returns

T

A random element from the collection.

Type Parameters

T

The 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

T

The 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

values IEnumerable<T>

A collection of enum values.

Returns

T

A random enum value from the collection.

Type Parameters

T

The 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

min T

The minimum enum value.

max T

The maximum enum value.

Returns

T

A random enum value between min and max.

Type Parameters

T

The 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

max int

The 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

min int

The minimum value.

max int

The maximum value.

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

min long

The minimum value.

max long

The maximum value.

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

min int

The minimum length for generating the salt.

max int

The 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

min TimeSpan

The minimum TimeSpan value.

max TimeSpan

The maximum TimeSpan value.

Returns

TimeSpan

A random TimeSpan between min and max.