Table of Contents

Class RandomHelper

Namespace
SunamoRandom
Assembly
SunamoRandom.dll

Provides methods for generating random values of various types including numbers, strings, bytes, and colors.

public static class RandomHelper
Inheritance
RandomHelper
Inherited Members

Properties

Type

The type of the RandomHelper class.

public static Type Type { get; set; }

Property Value

Type

Methods

RandomBool()

Generates a random boolean value.

public static bool RandomBool()

Returns

bool

A random boolean.

RandomByte(int, int)

Generates a random byte between from and toInclusive inclusive.

public static byte RandomByte(int from, int toInclusive)

Parameters

from int

Minimum value (inclusive).

toInclusive int

Maximum value (inclusive).

Returns

byte

A random byte.

RandomByte2(int, int)

Returns a number between from and to - 1 (exclusive upper bound). Useful for index calculations.

public static byte RandomByte2(int from, int to)

Parameters

from int

Minimum value (inclusive).

to int

Maximum value (exclusive).

Returns

byte

A random byte.

RandomBytes(int)

Generates an array of random bytes.

public static byte[] RandomBytes(int count)

Parameters

count int

Number of random bytes to generate.

Returns

byte[]

An array of random bytes.

RandomChar()

Returns a random character from all available character types.

public static char RandomChar()

Returns

char

A random character.

RandomCharWithoutSpecial()

Returns a random character from uppercase, lowercase letters and digits. Call ToLower when saving to DB.

public static char RandomCharWithoutSpecial()

Returns

char

A random alphanumeric character.

RandomColorPart(bool)

Generates a random color component byte value with default add value of 127.

public static byte RandomColorPart(bool isLight)

Parameters

isLight bool

Whether to generate a light color value.

Returns

byte

A random byte representing a color component.

RandomColorPart(bool, float)

Generates a random color component byte value.

public static byte RandomColorPart(bool isLight, float add)

Parameters

isLight bool

Whether to generate a light color value.

add float

Value to add to the light color base.

Returns

byte

A random byte representing a color component.

RandomDateTime(int)

Generates a random DateTime value up to the specified year.

public static DateTime RandomDateTime(int yearTo)

Parameters

yearTo int

Maximum year for the generated date.

Returns

DateTime

A random DateTime value.

RandomElementOfCollection(Array)

Returns a random element from an Array as a string.

public static string RandomElementOfCollection(Array array)

Parameters

array Array

The array to pick a random element from.

Returns

string

String representation of a random element.

RandomElementOfCollection(IList)

Returns a random element from an IList as a string.

public static string RandomElementOfCollection(IList list)

Parameters

list IList

The list to pick a random element from.

Returns

string

String representation of a random element.

RandomElementOfCollectionT<T>(IList<T>)

Returns a random element from a generic typed list.

public static T RandomElementOfCollectionT<T>(IList<T> list)

Parameters

list IList<T>

The list to pick a random element from.

Returns

T

A random element from the list, or default if empty.

Type Parameters

T

The type of elements in the list.

RandomEnum<T>()

Returns a random value from the specified enum type.

public static T RandomEnum<T>() where T : struct, Enum

Returns

T

A random enum value.

Type Parameters

T

The enum type.

RandomFloat(int, float, int)

Generates a random float value with the specified number of integer and decimal digits.

public static float RandomFloat(int decimalDigits, float maxValue, int maxIntegerDigits)

Parameters

decimalDigits int

Number of decimal digits (max 7).

maxValue float

Maximum allowed float value.

maxIntegerDigits int

Maximum number of integer digits.

Returns

float

A random float value not exceeding maxValue.

RandomInt()

Returns a random number between 0 and int.MaxValue - 1.

public static int RandomInt()

Returns

int

A random integer.

RandomInt(int)

Returns a random number between 0 and to - 1.

public static int RandomInt(int to)

Parameters

to int

Exclusive upper bound.

Returns

int

A random integer.

RandomInt(int, int)

Returns a random number between from and to inclusive.

public static int RandomInt(int from, int to)

Parameters

from int

Minimum value (inclusive).

to int

Maximum value (inclusive).

Returns

int

A random integer.

RandomInt2(int, int)

Returns a random number from from to to - 1 (exclusive upper bound).

public static int RandomInt2(int from, int to)

Parameters

from int

Minimum value (inclusive).

to int

Maximum value (exclusive).

Returns

int

A random integer.

RandomShort()

Returns a random number between 0 and short.MaxValue - 1.

public static short RandomShort()

Returns

short

A random short value.

RandomShort(short)

Returns a random number between 0 and to - 1.

public static short RandomShort(short to)

Parameters

to short

Exclusive upper bound.

Returns

short

A random short value.

RandomShort(short, short)

Returns a random number between from inclusive and to inclusive.

public static short RandomShort(short from, short to)

Parameters

from short

Minimum value (inclusive).

to short

Maximum value (inclusive).

Returns

short

A random short value.

RandomString()

Generates a random string of 7 characters.

public static string RandomString()

Returns

string

A random 7-character string.

RandomString(int)

Generates a random string of the specified length using all character types.

public static string RandomString(int length)

Parameters

length int

Desired length of the string (actual length is length - 1).

Returns

string

A random string.

RandomString(int, bool, bool, bool, bool)

Generates a random string composed of specified character types.

public static string RandomString(int length, bool isUpper, bool isLower, bool isNumeric, bool isSpecial)

Parameters

length int

Desired length of the string (actual length is length - 1).

isUpper bool

Whether to include uppercase characters.

isLower bool

Whether to include lowercase characters.

isNumeric bool

Whether to include numeric characters.

isSpecial bool

Whether to include special characters.

Returns

string

A random string of the specified character types.

RandomStringWithoutSpecial(int, bool)

Generates a random string without special characters containing only lowercase/uppercase letters and digits. Call ToLower when saving to DB. Newly calls ToLower automatically.

public static string RandomStringWithoutSpecial(int length, bool isAlsoUpper = false)

Parameters

length int

Desired length of the string (actual length is length - 1).

isAlsoUpper bool

Whether to include uppercase characters in the result.

Returns

string

A random alphanumeric string.