RandomNumberGenerator
An independent gameplay pseudorandom sequence generator.
An instance owns Seed/State and randomizes at construction. Assign Seed for a repeatable start; save/restore State to resume. Restore Seed before State when using both. Parameters and call order must match.
This does not establish cryptographic security, network determinism or future-version sequence identity. RandWeighted returns an index: validate finite nonnegative weights and a finite positive total. Never use the no-selection result −1 as an array index.
Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.
Constructors1
RandomNumberGeneratorConstructor
public RandomNumberGenerator();Creates an independent generator with its own Seed and State and automatically calls Randomize(). Zero values in reference metadata do not promise an initial zero seed. Set Seed before the first random call for repeatable output. Automatic initialization does not guarantee a unique seed for every instance.
Properties2
SeedProperty
public ulong Seed { get; set; }Sets the sequence start and resets state. Restore Seed before State when using both.
StateProperty
public ulong State { get; set; }State for resuming from a saved point. Restore a previously saved value after Seed; subsequent call order matters.
Methods12
GetSeedulong
public ulong GetSeed();Returns the seed that initialized the current stream, equivalent to reading Seed. Reading does not advance the generator. Randi and other random calls change State without turning Seed into the current state.
ulongLast initialization value from SetSeed/Seed or Randomize, as ulong.
GetStateulong
public ulong GetState();Returns the current state, equivalent to reading State, without advancing the generator. The saved value resumes a sequence; the initial Seed does not replace a State snapshot.
The example stores both ulong values as decimal strings. This preserves all 64 bits when passing through formats that may convert numbers to double or a signed 64-bit type. TryRestore validates both strings before changing the generator: Seed before State. Invalid input does not reset the original state.
using System.Globalization;
using Reckfell;
public static class RandomCheckpoint
{
public static string[] Capture(RandomNumberGenerator rng) =>
[
rng.Seed.ToString(CultureInfo.InvariantCulture),
rng.State.ToString(CultureInfo.InvariantCulture)
];
public static bool TryRestore(RandomNumberGenerator rng, string seedText, string stateText)
{
if (!ulong.TryParse(seedText, NumberStyles.None, CultureInfo.InvariantCulture, out ulong seed)
|| !ulong.TryParse(stateText, NumberStyles.None, CultureInfo.InvariantCulture, out ulong state))
return false;
rng.Seed = seed;
rng.State = state;
return true;
}
}After restoration, repeat the same methods with the same parameters and call order. The example was checked with a System.Text.Json string-array roundtrip; it does not guarantee network synchronization or future-version sequence identity.
ulongCurrent state snapshot as ulong; reading does not generate a random value.
RandWeighted2 overloads
public long RandWeighted(ReadOnlySpan<float> weights);Selects an index by relative weights. Validate finite nonnegative values and a finite positive total; no-selection result −1 is not an index.
| Name | Type | Default |
|---|---|---|
weights | ReadOnlySpan<float> | — |
weightsNonempty finite nonnegative weights with a finite positive total. A weight’s position determines the returned index. Empty or negative input produces native diagnostics; reject NaN/Infinity before calling.
longIndex 0..weights.Length - 1 for a valid choice, not the weight value. Returns -1 for no selection; check before indexing. A zero total selects nothing but advances the generator.
public long RandWeighted(float[] weights);Selects an index by relative weights. Validate finite nonnegative values and a finite positive total; no-selection result −1 is not an index.
| Name | Type | Default |
|---|---|---|
weights | float[] | — |
weightsNonempty finite nonnegative weights with a finite positive total. A weight’s position determines the returned index. Empty or negative input produces native diagnostics; reject NaN/Infinity before calling.
longIndex 0..weights.Length - 1 for a valid choice, not the weight value. Returns -1 for no selection; check before indexing. A zero total selects nothing but advances the generator.
Randffloat
public float Randf();Returns a pseudorandom float from 0 to 1, allowing both endpoints. Advances state; do not treat one Randf call as equivalent to one Randi for replay. Do not calculate an index as (int)(Randf() * Length): a result of 1 produces an out-of-bounds index.
floatfloat in [0, 1], not a ready-made array index.
RandfRangefloat
public float RandfRange(float from, float to);Computes Randf() * (to - from) + from as float. Uses ordinary float precision without a final Clamp; account for rounding error near the endpoints. Reversed bounds are not sorted: they reverse the transformation of a unit random sample.
The generator advances even when from == to, unlike RandiRange with equal bounds. Finite inputs alone do not prevent overflow: float.MaxValue - (-float.MaxValue) does not fit float, so the result can become Infinity or NaN.
| Name | Type | Default |
|---|---|---|
from | float | — |
to | float | — |
fromFirst range endpoint. Use a finite value and ensure to - from is representable.
toSecond endpoint; may be less than from. Intermediate calculations are not automatically saturated or clamped.
floatfloat after the stated transformation; overflow or invalid inputs can produce a nonfinite result.
Randfnfloat
public float Randfn(float mean = 0f, float deviation = 1f);Normally distributed value around mean with the chosen deviation. Not bounded by mean ± deviation; clamping changes the distribution.
| Name | Type | Default |
|---|---|---|
mean | float | 0f |
deviation | float | 1f |
meanDistribution mean, default 0. Use a finite value for ordinary gameplay calculations.
deviationDeviation scale, default 1. Use a finite nonnegative value for a standard deviation. 0 returns mean but still advances the generator; the API does not reject negative values.
floatNormally distributed sample with valid parameters; not restricted to mean ± deviation.
Randiuint
public uint Randi();Returns a pseudorandom uint and advances this instance’s state. For an array index, use RandiRange(0, Length - 1) only when Length > 0, rather than casting the full uint to int. The order and number of random calls affect replay.
uint32-bit unsigned value in the inclusive range 0..4294967295.
RandiRangeint
public int RandiRange(int from, int to);Returns an integer including both bounds. For a nonempty array index use 0..Length−1, not 0..Length.
| Name | Type | Default |
|---|---|---|
from | int | — |
to | int | — |
fromOne inclusive int bound. Order does not matter: the interval runs from the smaller to the larger value.
toOther inclusive int bound. For a nonempty array index, the upper bound is Length - 1.
intint from min(from, to) through max(from, to), including the full int range. Equal bounds return that value without advancing State.
Randomizevoid
public void Randomize();Reinitializes this instance using a value derived from time and its current state. Updates Seed and State. Do not call before every choice: it changes the stream’s start and interferes with replay. Calls do not guarantee unique seeds. Assign Seed explicitly for a repeatable run.
SetSeedvoid
public void SetSeed(ulong seed);Sets the initial value and reinitializes internal state, like assigning Seed. Setting the same seed again restarts its sequence. When restoring both seed and state, call SetSeed before SetState: reversing the order discards the saved point.
| Name | Type | Default |
|---|---|---|
seed | ulong | — |
seedInitial ulong value. Defines a repeatable start with the same algorithm, parameters and call order.
SetStatevoid
public void SetState(ulong state);Replaces internal state, like assigning State, without changing Seed. To resume, use a previously captured GetState()/State value; arbitrary input is not checked for suitability for your replay procedure. Assigning Seed afterward reinitializes state again.
| Name | Type | Default |
|---|---|---|
state | ulong | — |
statePreviously captured ulong state. Preserve its full precision when storing and reading it.