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
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
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
Returns
- byte
A random byte.
RandomBytes(int)
Generates an array of random bytes.
public static byte[] RandomBytes(int count)
Parameters
countintNumber 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
isLightboolWhether 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
isLightboolWhether to generate a light color value.
addfloatValue 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
yearTointMaximum 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
arrayArrayThe 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
listIListThe 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
listIList<T>The list to pick a random element from.
Returns
- T
A random element from the list, or default if empty.
Type Parameters
TThe 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
TThe 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
decimalDigitsintNumber of decimal digits (max 7).
maxValuefloatMaximum allowed float value.
maxIntegerDigitsintMaximum 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
tointExclusive 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
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
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
toshortExclusive 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
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
lengthintDesired 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
lengthintDesired length of the string (actual length is length - 1).
isUpperboolWhether to include uppercase characters.
isLowerboolWhether to include lowercase characters.
isNumericboolWhether to include numeric characters.
isSpecialboolWhether 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
lengthintDesired length of the string (actual length is length - 1).
isAlsoUpperboolWhether to include uppercase characters in the result.
Returns
- string
A random alphanumeric string.