Class Randomizer
Randomizer returns a set of random _values in a repeatable way, to allow re-running of tests if necessary. It extends the .NET Random class, providing random values for a much wider range of types.
The class is used internally by the framework to generate test case data and is also exposed for use by users through the TestContext.Random property.
public class Randomizer : Random
- Inheritance
-
Randomizer
- Inherited Members
Remarks
For consistency with the underlying Random Type, methods returning a single value use the prefix "Next..." Those without an argument return a non-negative value up to the full positive range of the Type. Overloads are provided for specifying a maximum or a range. Methods that return arrays or strings use the prefix "Get..." to avoid confusion with the single-value methods.
Constructors
Randomizer()
Default constructor
public Randomizer()
Randomizer(int)
Construct based on seed value
public Randomizer(int seed)
Parameters
seed
int
Fields
DefaultStringChars
Default characters for random functions.
public const string DefaultStringChars = "abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNOPQRSTUVWXYZ0123456789_"
Field Value
Remarks
Default characters are the English alphabet (uppercase & lowercase), arabic numerals, and underscore
Properties
InitialSeed
Initial seed used to create randomizers for this run
public static int InitialSeed { get; set; }
Property Value
Methods
CreateRandomizer()
Create a new Randomizer using the next seed available to ensure that each randomizer gives a unique sequence of values.
public static Randomizer CreateRandomizer()
Returns
GetRandomizer(MemberInfo)
Get a Randomizer for a particular member, returning one that has already been created if it exists. This ensures that the same _values are generated each time the tests are reloaded.
public static Randomizer GetRandomizer(MemberInfo member)
Parameters
member
MemberInfo
Returns
GetRandomizer(ParameterInfo)
Get a randomizer for a particular parameter, returning one that has already been created if it exists. This ensures that the same values are generated each time the tests are reloaded.
public static Randomizer GetRandomizer(ParameterInfo parameter)
Parameters
parameter
ParameterInfo
Returns
GetString()
Generate a random string based on the characters from the input string.
public string GetString()
Returns
- string
A random string of the default length
Remarks
Uses DefaultStringChars as the input character set
GetString(int)
Generate a random string based on the characters from the input string.
public string GetString(int outputLength)
Parameters
outputLength
intdesired length of output string.
Returns
- string
A random string of arbitrary length
Remarks
Uses DefaultStringChars as the input character set
GetString(int, string)
Generate a random string based on the characters from the input string.
public string GetString(int outputLength, string allowedChars)
Parameters
outputLength
intdesired length of output string.
allowedChars
stringstring representing the set of characters from which to construct the resulting string
Returns
- string
A random string of arbitrary length
NextBool()
Returns a random bool
public bool NextBool()
Returns
NextBool(double)
Returns a random bool based on the probablility a true result
public bool NextBool(double probability)
Parameters
probability
double
Returns
NextByte()
Returns a random Byte
public byte NextByte()
Returns
NextByte(byte)
Returns a random Byte less than the specified maximum.
public byte NextByte(byte max)
Parameters
max
byte
Returns
NextByte(byte, byte)
Returns a random Byte within a specified range
public byte NextByte(byte min, byte max)
Parameters
Returns
NextDecimal()
Returns a random decimal.
public decimal NextDecimal()
Returns
NextDecimal(decimal)
Returns a random decimal between positive zero and the specified maximum.
public decimal NextDecimal(decimal max)
Parameters
max
decimal
Returns
NextDecimal(decimal, decimal)
Returns a random decimal within a specified range, which is not permitted to exceed decimal.MaxVal in the current implementation.
public decimal NextDecimal(decimal min, decimal max)
Parameters
Returns
Remarks
A limitation of this implementation is that the range from min to max must not exceed decimal.MaxVal.
NextDouble(double)
Returns a random double between 0.0 and the specified maximum.
public double NextDouble(double max)
Parameters
max
double
Returns
NextDouble(double, double)
Returns a random double within a specified range.
public double NextDouble(double min, double max)
Parameters
Returns
NextEnum(Type)
Returns a random enum value of the specified Type as an object.
public object NextEnum(Type type)
Parameters
type
Type
Returns
NextEnum<T>()
Returns a random enum value of the specified Type.
public T NextEnum<T>()
Returns
- T
Type Parameters
T
NextFloat()
Returns a random float.
public float NextFloat()
Returns
NextFloat(float)
Returns a random float between 0.0 and the specified maximum.
public float NextFloat(float max)
Parameters
max
float
Returns
NextFloat(float, float)
Returns a random float within a specified range.
public float NextFloat(float min, float max)
Parameters
Returns
NextLong()
Returns a random long.
public long NextLong()
Returns
NextLong(long)
Returns a random long less than the specified maximum.
public long NextLong(long max)
Parameters
max
long
Returns
NextLong(long, long)
Returns a non-negative random long within a specified range.
public long NextLong(long min, long max)
Parameters
Returns
NextSByte()
Returns a random SByte
[CLSCompliant(false)]
public sbyte NextSByte()
Returns
NextSByte(sbyte)
Returns a random sbyte less than the specified maximum.
[CLSCompliant(false)]
public sbyte NextSByte(sbyte max)
Parameters
max
sbyte
Returns
NextSByte(sbyte, sbyte)
Returns a random sbyte within a specified range
[CLSCompliant(false)]
public sbyte NextSByte(sbyte min, sbyte max)
Parameters
Returns
NextShort()
Returns a non-negative random short.
public short NextShort()
Returns
NextShort(short)
Returns a non-negative random short less than the specified maximum.
public short NextShort(short max)
Parameters
max
short
Returns
NextShort(short, short)
Returns a non-negative random short within a specified range.
public short NextShort(short min, short max)
Parameters
Returns
NextUInt()
Returns a random unsigned int.
[CLSCompliant(false)]
public uint NextUInt()
Returns
NextUInt(uint)
Returns a random unsigned int less than the specified maximum.
[CLSCompliant(false)]
public uint NextUInt(uint max)
Parameters
max
uint
Returns
NextUInt(uint, uint)
Returns a random unsigned int within a specified range.
[CLSCompliant(false)]
public uint NextUInt(uint min, uint max)
Parameters
Returns
NextULong()
Returns a random ulong.
[CLSCompliant(false)]
public ulong NextULong()
Returns
NextULong(ulong)
Returns a random ulong less than the specified maximum.
[CLSCompliant(false)]
public ulong NextULong(ulong max)
Parameters
max
ulong
Returns
NextULong(ulong, ulong)
Returns a non-negative random long within a specified range.
[CLSCompliant(false)]
public ulong NextULong(ulong min, ulong max)
Parameters
Returns
NextUShort()
Returns a random unsigned short.
[CLSCompliant(false)]
public ushort NextUShort()
Returns
NextUShort(ushort)
Returns a random unsigned short less than the specified maximum.
[CLSCompliant(false)]
public ushort NextUShort(ushort max)
Parameters
max
ushort
Returns
NextUShort(ushort, ushort)
Returns a random unsigned short within a specified range.
[CLSCompliant(false)]
public ushort NextUShort(ushort min, ushort max)