Table of Contents

Class FakeRandom

Namespace
FEFF.TestFixtures.AspNetCore.Randomness
Assembly
FEFF.TestFixtures.AspNetCore.dll

A configurable random number generator for testing.
All public methods are thread-safe via lock().

public class FakeRandom : Random
Inheritance
FakeRandom
Inherited Members

Remarks

The default behavior uses a constant seed. Additional strategies such as fixed values can be applied via Int32Next, Int64Next, SingleNext, and DoubleNext.

Additional 'Next' strategies include:

  • Fixed
  • AutoIncrement (TODO)
  • ListRoundRobin (TODO)

Supported types:

When a 'Next' strategy returns a value that is out of bounds of a defined method contract (e.g., Next(min,max)), a 'NormalizationStrategy' can handle this. The default strategy throws InvalidOperationException.

Available 'NormalizationStrategy' implementations:

Constructors

FakeRandom()

Creates a new FakeRandom with a constant seed of 1.

public FakeRandom()

Properties

ByteNext

Gets or sets the strategy for generating byte values, or null to use the default.

public INextStrategy<byte>? ByteNext { get; set; }

Property Value

INextStrategy<byte>

DoubleNext

Gets or sets the strategy for generating double values, or null to use the default.

public INextStrategy<double>? DoubleNext { get; set; }

Property Value

INextStrategy<double>

Int32Next

Gets or sets the strategy for generating int values, or null to use the default.

public INextStrategy<int>? Int32Next { get; set; }

Property Value

INextStrategy<int>

Int64Next

Gets or sets the strategy for generating long values, or null to use the default.

public INextStrategy<long>? Int64Next { get; set; }

Property Value

INextStrategy<long>

NormalizationStrategy

Gets or sets the normalization strategy used when a generated value falls outside the valid range of the target Random method.

public INormalizationStrategy NormalizationStrategy { get; set; }

Property Value

INormalizationStrategy

Remarks

The default is ThrowNormalization, which throws InvalidOperationException on out-of-range values.

Exceptions

ArgumentNullException

Thrown when setting the property to null.

SingleNext

Gets or sets the strategy for generating float values, or null to use the default.

public INextStrategy<float>? SingleNext { get; set; }

Property Value

INextStrategy<float>

Methods

Next()

Returns a non-negative random integer.

public override int Next()

Returns

int

A 32-bit signed integer that is greater than or equal to 0 and less than Int32.MaxValue.

Next(int)

Returns a non-negative random integer that is less than the specified maximum.

public override int Next(int maxValue)

Parameters

maxValue int

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

Returns

int

A 32-bit signed integer that is greater than or equal to 0, and less than maxValue; that is, the range of return values ordinarily includes 0 but not maxValue. However, if maxValue equals 0, 0 is returned.

Exceptions

ArgumentOutOfRangeException

maxValue is less than 0.

Next(int, int)

Returns a random integer that is within a specified range.

public override int Next(int minValue, int maxValue)

Parameters

minValue int

The inclusive lower bound of the random number returned.

maxValue int

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

Returns

int

A 32-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.

Exceptions

ArgumentOutOfRangeException

minValue is greater than maxValue.

NextBytes(byte[])

Fills the elements of a specified array of bytes with random numbers.

public override void NextBytes(byte[] buffer)

Parameters

buffer byte[]

The array to be filled with random numbers.

Exceptions

ArgumentNullException

buffer is null.

NextBytes(Span<byte>)

Fills the elements of a specified span of bytes with random numbers.

public override void NextBytes(Span<byte> buffer)

Parameters

buffer Span<byte>

The array to be filled with random numbers.

NextDouble()

Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.

public override double NextDouble()

Returns

double

A double-precision floating point number that is greater than or equal to 0.0, and less than 1.0.

NextInt64()

Returns a non-negative random integer.

public override long NextInt64()

Returns

long

A 64-bit signed integer that is greater than or equal to 0 and less than Int64.MaxValue.

NextInt64(long)

Returns a non-negative random integer that is less than the specified maximum.

public override long NextInt64(long maxValue)

Parameters

maxValue long

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

Returns

long

A 64-bit signed integer that is greater than or equal to 0, and less than maxValue; that is, the range of return values ordinarily includes 0 but not maxValue. However, if maxValue equals 0, maxValue is returned.

Exceptions

ArgumentOutOfRangeException

maxValue is less than 0.

NextInt64(long, long)

Returns a random integer that is within a specified range.

public override long NextInt64(long minValue, long maxValue)

Parameters

minValue long

The inclusive lower bound of the random number returned.

maxValue long

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

Returns

long

A 64-bit signed integer greater than or equal to minValue and less than maxValue; that is, the range of return values includes minValue but not maxValue. If minValue equals maxValue, minValue is returned.

Exceptions

ArgumentOutOfRangeException

minValue is greater than maxValue.

NextSingle()

Returns a random floating-point number that is greater than or equal to 0.0, and less than 1.0.

public override float NextSingle()

Returns

float

A single-precision floating point number that is greater than or equal to 0.0, and less than 1.0.