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:
- ThrowNormalization — throws on out-of-range values
- ReturnAsIsNormalization — returns the value unchanged
- AutoFitNormalization (TODO)
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
DoubleNext
Gets or sets the strategy for generating double values, or null to use the default.
public INextStrategy<double>? DoubleNext { get; set; }
Property Value
Int32Next
Gets or sets the strategy for generating int values, or null to use the default.
public INextStrategy<int>? Int32Next { get; set; }
Property Value
Int64Next
Gets or sets the strategy for generating long values, or null to use the default.
public INextStrategy<long>? Int64Next { get; set; }
Property Value
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
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
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
maxValueintThe exclusive upper bound of the random number to be generated.
maxValuemust 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 notmaxValue. However, ifmaxValueequals 0, 0 is returned.
Exceptions
- ArgumentOutOfRangeException
maxValueis 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
minValueintThe inclusive lower bound of the random number returned.
maxValueintThe exclusive upper bound of the random number returned.
maxValuemust be greater than or equal tominValue.
Returns
- int
A 32-bit signed integer greater than or equal to
minValueand less thanmaxValue; that is, the range of return values includesminValuebut notmaxValue. IfminValueequalsmaxValue,minValueis returned.
Exceptions
- ArgumentOutOfRangeException
minValueis greater thanmaxValue.
NextBytes(byte[])
Fills the elements of a specified array of bytes with random numbers.
public override void NextBytes(byte[] buffer)
Parameters
bufferbyte[]The array to be filled with random numbers.
Exceptions
- ArgumentNullException
bufferis null.
NextBytes(Span<byte>)
Fills the elements of a specified span of bytes with random numbers.
public override void NextBytes(Span<byte> buffer)
Parameters
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
maxValuelongThe exclusive upper bound of the random number to be generated.
maxValuemust 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 notmaxValue. However, ifmaxValueequals 0,maxValueis returned.
Exceptions
- ArgumentOutOfRangeException
maxValueis 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
minValuelongThe inclusive lower bound of the random number returned.
maxValuelongThe exclusive upper bound of the random number returned.
maxValuemust be greater than or equal tominValue.
Returns
- long
A 64-bit signed integer greater than or equal to
minValueand less thanmaxValue; that is, the range of return values includesminValuebut notmaxValue. If minValue equalsmaxValue,minValueis returned.
Exceptions
- ArgumentOutOfRangeException
minValueis greater thanmaxValue.
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.