Table of Contents

Class StreamBackedRandom

Namespace
Mrg32k3a.NET
Assembly
Mrg32k3a.NET.dll

Presents a RandomStream as a Random, so a stream can be handed to any API or library that asks for the framework type.

public sealed class StreamBackedRandom : Random
Inheritance
StreamBackedRandom
Inherited Members

Remarks

The adapter holds the stream rather than copying it, so draws taken through the adapter and draws taken directly from the stream come from the same sequence and advance the same state.

Like the stream it wraps, an adapter is not thread safe.

Constructors

StreamBackedRandom(RandomStream)

Wraps an existing stream.

public StreamBackedRandom(RandomStream stream)

Parameters

stream RandomStream

The stream to present as a Random.

Exceptions

ArgumentNullException

stream is null.

Properties

Stream

Gets the stream behind this adapter.

public RandomStream Stream { get; }

Property Value

RandomStream

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.

Remarks

Draws close enough to one round up to 1f when narrowed, so those are pulled back to the largest single below one to keep the half-open range.

Sample()

Returns a random floating-point number between 0.0 and 1.0.

protected override double Sample()

Returns

double

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