Class SystemRandomSource
An IRandomSource adapter that wraps an instance of Random, allowing it to participate in the Scylla random subsystem alongside the native algorithm structs.
Implements
Inherited Members
Namespace: Scylla.Core.Util.Random
Assembly: ScyllaCore.dll
Syntax
public sealed class SystemRandomSource : IRandomSource
Remarks
This adapter is intended for interoperability scenarios where a Random instance already exists in the codebase (for example, one created by a third-party library) and needs to be plugged into an API that expects IRandomSource. For new code, prefer one of the native algorithm structs such as Xoshiro256StarStar or PCG32, which offer deterministic seeding, state save/restore, and better statistical quality.
Random is not thread-safe. Do not share a single SystemRandomSource instance across multiple threads without external synchronization, or use separate instances per thread.
The output is not cryptographically secure. For security-sensitive use cases, use CryptoRandomSource instead.
Constructors
SystemRandomSource(Random)
Initializes a new SystemRandomSource that delegates all random number generation to the provided Random instance.
Declaration
public SystemRandomSource(Random random)
Parameters
| Type | Name | Description |
|---|---|---|
| Random | random | The Random instance to wrap. Must not be |
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
Methods
NextBytes(byte[], int, int)
Fills a contiguous region of a byte array with pseudo-random bytes produced by the underlying Random instance.
Declaration
public void NextBytes(byte[] buffer, int offset = 0, int count = -1)
Parameters
| Type | Name | Description |
|---|---|---|
| byte[] | buffer | The destination byte array. Must not be |
| int | offset | The zero-based starting index within |
| int | count | The number of bytes to fill. Pass |
Remarks
When offset is 0 and count equals
buffer.Length, NextBytes(byte[]) is called
directly on the provided buffer with no intermediate copy. For partial fills a
temporary array of length count is allocated and then
block-copied into the target region.
Exceptions
| Type | Condition |
|---|---|
| ArgumentNullException | Thrown when |
| ArgumentOutOfRangeException | Thrown when |
NextUInt32()
Returns an unsigned 32-bit pseudo-random integer produced by filling a 4-byte buffer via NextBytes(byte[]) and interpreting the result as a little-endian uint.
Declaration
public uint NextUInt32()
Returns
| Type | Description |
|---|---|
| uint | A pseudo-random uint in the range |
Remarks
Allocates a 4-byte temporary buffer for each call. When generating large amounts of random data, prefer NextBytes(byte[], int, int) to avoid per-call allocations.
NextUInt64()
Returns an unsigned 64-bit pseudo-random integer produced by filling an 8-byte buffer via NextBytes(byte[]) and interpreting the result as a little-endian ulong.
Declaration
public ulong NextUInt64()
Returns
| Type | Description |
|---|---|
| ulong | A pseudo-random ulong in the range |
Remarks
Allocates an 8-byte temporary buffer for each call. When generating large amounts of random data, prefer NextBytes(byte[], int, int) to avoid per-call allocations.