Search Results for

    Show / Hide Table of Contents

    Class RandomUtil

    Summary description for RandomUtil.

    Inheritance
    System.Object
    RandomUtil
    Inherited Members
    System.Object.ToString()
    System.Object.Equals(System.Object)
    System.Object.Equals(System.Object, System.Object)
    System.Object.ReferenceEquals(System.Object, System.Object)
    System.Object.GetHashCode()
    System.Object.GetType()
    System.Object.MemberwiseClone()
    Namespace: ImageVault.Common.Lib
    Assembly: ImageVault.Common.dll
    Syntax
    public static class RandomUtil

    Fields

    DefaultArrayLength

    Gets the DefaultArrayLength of the RandomUtil

    Declaration
    public const int DefaultArrayLength = 5
    Field Value
    Type Description
    System.Int32

    DefaultPercentageNullableNullOccurrance

    Gets the DefaultPercentageNullableNullOccurrance of the RandomUtil

    Declaration
    [Obsolete("Use DefaultPercentageNullableNullOccurrence instead")]
    public const int DefaultPercentageNullableNullOccurrance = 10
    Field Value
    Type Description
    System.Int32

    DefaultPercentageNullableNullOccurrence

    Gets the DefaultPercentageNullableNullOccurrence of the RandomUtil

    Declaration
    public const int DefaultPercentageNullableNullOccurrence = 10
    Field Value
    Type Description
    System.Int32

    DefaultStringLength

    Gets the DefaultStringLength of the RandomUtil

    Declaration
    public const int DefaultStringLength = 20
    Field Value
    Type Description
    System.Int32

    DefaultStringLengthVariation

    Gets the DefaultStringLengthVariation of the RandomUtil

    Declaration
    public const int DefaultStringLengthVariation = 5
    Field Value
    Type Description
    System.Int32

    Methods

    GetRandomBoolean()

    Gets a random boolean value

    Declaration
    public static bool GetRandomBoolean()
    Returns
    Type Description
    System.Boolean

    GetRandomByte()

    Returns a random byte

    Declaration
    public static byte GetRandomByte()
    Returns
    Type Description
    System.Byte

    GetRandomChar()

    Returns a random character

    Declaration
    public static char GetRandomChar()
    Returns
    Type Description
    System.Char

    GetRandomColor()

    Gets a random color

    Declaration
    public static Color GetRandomColor()
    Returns
    Type Description
    System.Drawing.Color

    GetRandomDateTime()

    Returns a randomized DateTime

    Declaration
    public static DateTime GetRandomDateTime()
    Returns
    Type Description
    System.DateTime

    GetRandomDateTime(DateTime, DateTime)

    Declaration
    public static DateTime GetRandomDateTime(DateTime minDate, DateTime maxDate)
    Parameters
    Type Name Description
    System.DateTime minDate
    System.DateTime maxDate
    Returns
    Type Description
    System.DateTime

    GetRandomDecimal()

    Gets a randomized decimal

    Declaration
    public static decimal GetRandomDecimal()
    Returns
    Type Description
    System.Decimal

    GetRandomDouble()

    Gets a randomized double

    Declaration
    public static double GetRandomDouble()
    Returns
    Type Description
    System.Double

    GetRandomDouble(Double)

    Gets a random double value from zero to the supplied maxvalue

    Declaration
    public static double GetRandomDouble(double maxValue)
    Parameters
    Type Name Description
    System.Double maxValue

    The limit of the returned value

    Returns
    Type Description
    System.Double

    The randomized System.Double

    GetRandomEnum(Type)

    Gets a randomized enum value

    Declaration
    public static Enum GetRandomEnum(Type type)
    Parameters
    Type Name Description
    System.Type type

    The type of enum to randomize a value from

    Returns
    Type Description
    System.Enum

    The randomized System.Enum

    GetRandomInt16()

    Returns a random Int16

    Declaration
    public static short GetRandomInt16()
    Returns
    Type Description
    System.Int16

    GetRandomInt32()

    Returns a random System.Int32

    Declaration
    public static int GetRandomInt32()
    Returns
    Type Description
    System.Int32

    The random System.Int32

    GetRandomInt32(Int32)

    Returns a non negative random number in the given interval

    Declaration
    public static int GetRandomInt32(int maxValue)
    Parameters
    Type Name Description
    System.Int32 maxValue

    The exclusive upper bound of the random number returned. maxValue must be greater than or equal to 0.

    Returns
    Type Description
    System.Int32

    The random System.Int32

    GetRandomInt32(Int32, Int32)

    Returns a random number in the given interval

    Declaration
    public static int GetRandomInt32(int minValue, int maxValue)
    Parameters
    Type Name Description
    System.Int32 minValue

    The inclusive lower bound of the random number returned

    System.Int32 maxValue

    The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minvalue.

    Returns
    Type Description
    System.Int32

    The random System.Int32

    GetRandomInt32(Int32, Int32, Int32[])

    Returns a random number in the given interval

    Declaration
    public static int GetRandomInt32(int minValue, int maxValue, params int[] notValidValues)
    Parameters
    Type Name Description
    System.Int32 minValue

    The inclusive lower bound of the random number returned

    System.Int32 maxValue

    The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minvalue.

    System.Int32[] notValidValues

    A System.Int32 array of non valid values

    Returns
    Type Description
    System.Int32

    The random System.Int32

    GetRandomInt32Except(Int32[])

    Returns a random integer that is not one of the supplied ones

    Declaration
    public static int GetRandomInt32Except(params int[] notValidValues)
    Parameters
    Type Name Description
    System.Int32[] notValidValues

    A System.Int32 array of non valid values

    Returns
    Type Description
    System.Int32

    The random System.Int32

    GetRandomInt64()

    returns a random Int64

    Declaration
    public static long GetRandomInt64()
    Returns
    Type Description
    System.Int64

    GetRandomInt64(Int64)

    Returns a random value between 0 and maxValue

    Declaration
    public static long GetRandomInt64(long maxValue)
    Parameters
    Type Name Description
    System.Int64 maxValue
    Returns
    Type Description
    System.Int64

    GetRandomInt64(Int64, Int64)

    Returns a random value between minValue and maxValue

    Declaration
    public static long GetRandomInt64(long minValue, long maxValue)
    Parameters
    Type Name Description
    System.Int64 minValue
    System.Int64 maxValue
    Returns
    Type Description
    System.Int64

    GetRandomItem(IList)

    Gets a random item from the supplied list

    Declaration
    public static object GetRandomItem(IList list)
    Parameters
    Type Name Description
    System.Collections.IList list

    The System.Collections.IList to get the item from.

    Returns
    Type Description
    System.Object

    The selected item or null if the list is null or empty

    GetRandomItem(IList, Int32, Int32)

    Gets a random item from the list

    Declaration
    public static object GetRandomItem(IList list, int startIndex, int length)
    Parameters
    Type Name Description
    System.Collections.IList list

    The System.Collections.Generic.IList<T> to get the item from

    System.Int32 startIndex

    The start position where we should start our selection

    System.Int32 length

    The number of items to include in the selection

    Returns
    Type Description
    System.Object

    The selected item or null if the list is null or empty

    Exceptions
    Type Condition
    System.ArgumentOutOfRangeException

    If length is out of range (Has to be a non negative value).

    GetRandomItem(String)

    Gets a random character from a string

    Declaration
    public static char GetRandomItem(string s)
    Parameters
    Type Name Description
    System.String s

    The string to randomize an item from

    Returns
    Type Description
    System.Char
    Exceptions
    Type Condition
    System.ArgumentException

    If cannot get a random char in a null or empty string.

    GetRandomItem<T>(IList<T>)

    Gets a random item from the list

    Declaration
    public static T GetRandomItem<T>(IList<T> list)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<T> list

    The System.Collections.Generic.IList<T> to get the item from

    Returns
    Type Description
    T

    The selected T or default(T) if the list is null or empty

    Type Parameters
    Name Description
    T

    The type of item to get.

    GetRandomItem<T>(IList<T>, Int32, Int32)

    Gets a random item from the list

    Declaration
    public static T GetRandomItem<T>(IList<T> list, int startIndex, int length)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<T> list

    The System.Collections.Generic.IList<T> to get the item from

    System.Int32 startIndex

    The start position where we should start our selection

    System.Int32 length

    The number of items to include in the selection

    Returns
    Type Description
    T

    The selected T or default(T) if the list is null or empty

    Type Parameters
    Name Description
    T

    The type of item contained in the list

    Exceptions
    Type Condition
    System.ArgumentOutOfRangeException

    If length is out of range (Has to be a non negative value).

    GetRandomItems<T>(IList<T>, Int32)

    Declaration
    public static IList<T> GetRandomItems<T>(IList<T> items, int no)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<T> items
    System.Int32 no
    Returns
    Type Description
    System.Collections.Generic.IList<T>
    Type Parameters
    Name Description
    T
    Exceptions
    Type Condition
    System.ArgumentNullException

    If items is null.

    System.ArgumentException

    If cannot get .

    GetRandomLetterAndDigit()

    Gets a randomized letter or digit sign.

    Declaration
    public static char GetRandomLetterAndDigit()
    Returns
    Type Description
    System.Char

    The randomized letter or digit sign.

    GetRandomLetterAndDigitString(Int32)

    Generates a string of randomized letters and digits of the supplied length

    Declaration
    public static string GetRandomLetterAndDigitString(int length)
    Parameters
    Type Name Description
    System.Int32 length

    The length of the randomized string

    Returns
    Type Description
    System.String

    The randomized System.String

    GetRandomLetterAndDigitString(Int32, String[])

    Generates a string of randomized letters and digits of the supplied length

    Declaration
    public static string GetRandomLetterAndDigitString(int length, params string[] notValidValues)
    Parameters
    Type Name Description
    System.Int32 length

    The length of the randomized string

    System.String[] notValidValues

    The System.String array that contains the values that the randomized string shouldn't have.

    Returns
    Type Description
    System.String

    The randomized System.String

    GetRandomLetterAndDigitString(String[])

    Generates a string of randomized letters and digits

    Declaration
    public static string GetRandomLetterAndDigitString(params string[] notValidValues)
    Parameters
    Type Name Description
    System.String[] notValidValues

    The System.String array that contains the values that the randomized string shouldn't have.

    Returns
    Type Description
    System.String

    The randomized System.String

    GetRandomLetterString(Int32, Int32)

    Gets a randomized letter string

    Declaration
    public static string GetRandomLetterString(int length, int delta)
    Parameters
    Type Name Description
    System.Int32 length

    The approximate length of the string

    System.Int32 delta

    The variation delta.

    Returns
    Type Description
    System.String

    The randomized System.String

    GetRandomNullableBoolean()

    Gets a random nullable boolean

    Declaration
    public static bool? GetRandomNullableBoolean()
    Returns
    Type Description
    System.Nullable<System.Boolean>

    GetRandomObject(Type)

    Gets a random object instance of the specified type

    Declaration
    public static object GetRandomObject(Type type)
    Parameters
    Type Name Description
    System.Type type

    The System.Type of object to instance

    Returns
    Type Description
    System.Object

    The instanced object

    GetRandomSByte()

    Returns a random System.SByte

    Declaration
    [CLSCompliant(false)]
    public static sbyte GetRandomSByte()
    Returns
    Type Description
    System.SByte

    The random System.SByte

    GetRandomSByte(SByte)

    Returns a non negative random number in the given interval

    Declaration
    [CLSCompliant(false)]
    public static sbyte GetRandomSByte(sbyte maxValue)
    Parameters
    Type Name Description
    System.SByte maxValue

    The exclusive upper bound of the random number returned. maxValue must be greater than or equal to 0.

    Returns
    Type Description
    System.SByte

    The random System.SByte

    GetRandomSByte(SByte, SByte)

    Returns a random number in the given interval

    Declaration
    [CLSCompliant(false)]
    public static sbyte GetRandomSByte(sbyte minValue, sbyte maxValue)
    Parameters
    Type Name Description
    System.SByte minValue

    The inclusive lower bound of the random number returned

    System.SByte maxValue

    The exclusive upper bound of the random number returned. maxValue must be greater than or equal to minvalue.

    Returns
    Type Description
    System.SByte

    The random System.SByte

    GetRandomSingle()

    Returns a random single

    Declaration
    public static float GetRandomSingle()
    Returns
    Type Description
    System.Single

    The random System.Single

    GetRandomSingle(Single)

    Returns a random single

    Declaration
    public static float GetRandomSingle(float maxValue)
    Parameters
    Type Name Description
    System.Single maxValue

    The maximum System.Single to return

    Returns
    Type Description
    System.Single

    The random System.Single

    GetRandomSingle(Single, Single)

    Returns a random value between minValue and maxValue

    Declaration
    public static float GetRandomSingle(float minValue, float maxValue)
    Parameters
    Type Name Description
    System.Single minValue

    The minimum System.Single value to return

    System.Single maxValue

    The maximum System.Single value to return

    Returns
    Type Description
    System.Single

    The randomized System.Single

    GetRandomString()

    Gets a randomized string of default length

    Declaration
    public static string GetRandomString()
    Returns
    Type Description
    System.String

    The randomized length

    GetRandomString(Int32)

    Gets a randomized string of the approximate length

    Declaration
    public static string GetRandomString(int length)
    Parameters
    Type Name Description
    System.Int32 length

    The approximate length of the string

    Returns
    Type Description
    System.String

    The randomized string

    GetRandomString(Int32, Int32)

    Gets a randomized string of the approximate length

    Declaration
    public static string GetRandomString(int length, int delta)
    Parameters
    Type Name Description
    System.Int32 length

    The approximate length of the string

    System.Int32 delta

    The variation delta.

    Returns
    Type Description
    System.String

    The randomized length

    GetRandomString(Int32, Int32, String, String[])

    Returns a randomized string of the number of characters that is supplied.

    Declaration
    public static string GetRandomString(int length, int delta, string validCharacters, params string[] notValidValues)
    Parameters
    Type Name Description
    System.Int32 length

    The approximate length of the string

    System.Int32 delta

    The variation delta.

    System.String validCharacters

    A string containing the possible characters in the string

    System.String[] notValidValues

    The System.String array that contains the values that the randomized string shouldn't have.

    Returns
    Type Description
    System.String

    The randomized System.String

    GetRandomString(Int32, String)

    Returns a randomized string of the number of characters that is supplied.

    Declaration
    public static string GetRandomString(int length, string validCharacters)
    Parameters
    Type Name Description
    System.Int32 length

    The approximate length of the string

    System.String validCharacters

    A string containing the possible characters in the string

    Returns
    Type Description
    System.String

    The randomized System.String

    GetRandomString(Nullable<Int32>)

    Gets a random string with an exact length

    Declaration
    public static string GetRandomString(int? exactLength)
    Parameters
    Type Name Description
    System.Nullable<System.Int32> exactLength

    The length of the string, null will return null, 0 will return System.String.Empty.

    Returns
    Type Description
    System.String

    The random string

    GetRandomTimeSpan()

    Gets a randomized TimeSpan

    Declaration
    public static object GetRandomTimeSpan()
    Returns
    Type Description
    System.Object

    IsTrue(Int32)

    Returns true if a randomized number from 1 to 100 is less or equal to the given value.

    Declaration
    public static bool IsTrue(int percent)
    Parameters
    Type Name Description
    System.Int32 percent

    The percentage that this method will return true

    Returns
    Type Description
    System.Boolean

    True if it is true, otherwise false.

    Random<T>(IList<T>)

    Declaration
    public static T Random<T>(this IList<T> list)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<T> list
    Returns
    Type Description
    T
    Type Parameters
    Name Description
    T
    Exceptions
    Type Condition
    System.ArgumentException

    If cannot select a random item on an empty list.

    Random<T>(IList<T>, Int32)

    Declaration
    public static IList<T> Random<T>(this IList<T> list, int count)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<T> list
    System.Int32 count
    Returns
    Type Description
    System.Collections.Generic.IList<T>
    Type Parameters
    Name Description
    T

    Random<TItem, TReturn>(IList<TItem>, Func<TItem, TReturn>)

    Selects a random item from the list and calls the supplied function

    Declaration
    public static TReturn Random<TItem, TReturn>(this IList<TItem> list, Func<TItem, TReturn> func)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<TItem> list
    System.Func<TItem, TReturn> func
    Returns
    Type Description
    TReturn
    Type Parameters
    Name Description
    TItem
    TReturn
    Exceptions
    Type Condition
    System.ArgumentException

    If cannot select a random item on an empty list.

    Random<TItem, TReturn>(IList<TItem>, Int32, Func<TItem, TReturn>)

    Declaration
    public static IList<TReturn> Random<TItem, TReturn>(this IList<TItem> list, int count, Func<TItem, TReturn> func)
    Parameters
    Type Name Description
    System.Collections.Generic.IList<TItem> list
    System.Int32 count
    System.Func<TItem, TReturn> func
    Returns
    Type Description
    System.Collections.Generic.IList<TReturn>
    Type Parameters
    Name Description
    TItem
    TReturn
    Exceptions
    Type Condition
    System.ArgumentException

    If cannot select a random item on an empty list.

    In This Article
    Back to top (c) Meriworks 2002-2022