Table of Contents

Class Randomizer

Namespace
NUnit.Framework.Internal
Assembly
nunit.framework.dll

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

string

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

int

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

Randomizer

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

Randomizer

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

Randomizer

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 int

desired 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 int

desired length of output string.

allowedChars string

string 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

bool

NextBool(double)

Returns a random bool based on the probablility a true result

public bool NextBool(double probability)

Parameters

probability double

Returns

bool

NextByte()

Returns a random Byte

public byte NextByte()

Returns

byte

NextByte(byte)

Returns a random Byte less than the specified maximum.

public byte NextByte(byte max)

Parameters

max byte

Returns

byte

NextByte(byte, byte)

Returns a random Byte within a specified range

public byte NextByte(byte min, byte max)

Parameters

min byte
max byte

Returns

byte

NextDecimal()

Returns a random decimal.

public decimal NextDecimal()

Returns

decimal

NextDecimal(decimal)

Returns a random decimal between positive zero and the specified maximum.

public decimal NextDecimal(decimal max)

Parameters

max decimal

Returns

decimal

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

min decimal
max decimal

Returns

decimal

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

double

NextDouble(double, double)

Returns a random double within a specified range.

public double NextDouble(double min, double max)

Parameters

min double
max double

Returns

double

NextEnum(Type)

Returns a random enum value of the specified Type as an object.

public object NextEnum(Type type)

Parameters

type Type

Returns

object

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

float

NextFloat(float)

Returns a random float between 0.0 and the specified maximum.

public float NextFloat(float max)

Parameters

max float

Returns

float

NextFloat(float, float)

Returns a random float within a specified range.

public float NextFloat(float min, float max)

Parameters

min float
max float

Returns

float

NextLong()

Returns a random long.

public long NextLong()

Returns

long

NextLong(long)

Returns a random long less than the specified maximum.

public long NextLong(long max)

Parameters

max long

Returns

long

NextLong(long, long)

Returns a non-negative random long within a specified range.

public long NextLong(long min, long max)

Parameters

min long
max long

Returns

long

NextSByte()

Returns a random SByte

[CLSCompliant(false)]
public sbyte NextSByte()

Returns

sbyte

NextSByte(sbyte)

Returns a random sbyte less than the specified maximum.

[CLSCompliant(false)]
public sbyte NextSByte(sbyte max)

Parameters

max sbyte

Returns

sbyte

NextSByte(sbyte, sbyte)

Returns a random sbyte within a specified range

[CLSCompliant(false)]
public sbyte NextSByte(sbyte min, sbyte max)

Parameters

min sbyte
max sbyte

Returns

sbyte

NextShort()

Returns a non-negative random short.

public short NextShort()

Returns

short

NextShort(short)

Returns a non-negative random short less than the specified maximum.

public short NextShort(short max)

Parameters

max short

Returns

short

NextShort(short, short)

Returns a non-negative random short within a specified range.

public short NextShort(short min, short max)

Parameters

min short
max short

Returns

short

NextUInt()

Returns a random unsigned int.

[CLSCompliant(false)]
public uint NextUInt()

Returns

uint

NextUInt(uint)

Returns a random unsigned int less than the specified maximum.

[CLSCompliant(false)]
public uint NextUInt(uint max)

Parameters

max uint

Returns

uint

NextUInt(uint, uint)

Returns a random unsigned int within a specified range.

[CLSCompliant(false)]
public uint NextUInt(uint min, uint max)

Parameters

min uint
max uint

Returns

uint

NextULong()

Returns a random ulong.

[CLSCompliant(false)]
public ulong NextULong()

Returns

ulong

NextULong(ulong)

Returns a random ulong less than the specified maximum.

[CLSCompliant(false)]
public ulong NextULong(ulong max)

Parameters

max ulong

Returns

ulong

NextULong(ulong, ulong)

Returns a non-negative random long within a specified range.

[CLSCompliant(false)]
public ulong NextULong(ulong min, ulong max)

Parameters

min ulong
max ulong

Returns

ulong

NextUShort()

Returns a random unsigned short.

[CLSCompliant(false)]
public ushort NextUShort()

Returns

ushort

NextUShort(ushort)

Returns a random unsigned short less than the specified maximum.

[CLSCompliant(false)]
public ushort NextUShort(ushort max)

Parameters

max ushort

Returns

ushort

NextUShort(ushort, ushort)

Returns a random unsigned short within a specified range.

[CLSCompliant(false)]
public ushort NextUShort(ushort min, ushort max)

Parameters

min ushort
max ushort

Returns

ushort