reckfellENGINE

RandomNumberGenerator

An independent gameplay pseudorandom sequence generator.

On this page
class RandomNumberGeneratorReckfellSharpInherits RefCounted

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.

Practical guide.

Declarations checked against C# SDK 1.0.0. Expand a name to see declarations and documentation.

Constructors1

RandomNumberGeneratorConstructor
# RandomNumberGenerator
C#
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
# Seed
C#
public ulong Seed { get; set; }

Sets the sequence start and resets state. Restore Seed before State when using both.

StateProperty
# State
C#
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
# GetSeed
C#
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.

Returns ulong

Last initialization value from SetSeed/Seed or Randomize, as ulong.

GetStateulong
# GetState
C#
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.

C#
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.

Returns ulong

Current state snapshot as ulong; reading does not generate a random value.

RandWeighted2 overloads
# RandWeighted · 1/2
C#
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.

Parameters
NameTypeDefault
weightsReadOnlySpan<float>—
weights

Nonempty 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.

Returns long

Index 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.

# RandWeighted · 2/2
C#
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.

Parameters
NameTypeDefault
weightsfloat[]—
weights

Nonempty 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.

Returns long

Index 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
# Randf
C#
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.

Returns float

float in [0, 1], not a ready-made array index.

RandfRangefloat
# RandfRange
C#
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.

Parameters
NameTypeDefault
fromfloat—
tofloat—
from

First range endpoint. Use a finite value and ensure to - from is representable.

to

Second endpoint; may be less than from. Intermediate calculations are not automatically saturated or clamped.

Returns float

float after the stated transformation; overflow or invalid inputs can produce a nonfinite result.

Randfnfloat
# Randfn
C#
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.

Parameters
NameTypeDefault
meanfloat0f
deviationfloat1f
mean

Distribution mean, default 0. Use a finite value for ordinary gameplay calculations.

deviation

Deviation 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.

Returns float

Normally distributed sample with valid parameters; not restricted to mean ± deviation.

Randiuint
# Randi
C#
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.

Returns uint

32-bit unsigned value in the inclusive range 0..4294967295.

RandiRangeint
# RandiRange
C#
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.

Parameters
NameTypeDefault
fromint—
toint—
from

One inclusive int bound. Order does not matter: the interval runs from the smaller to the larger value.

to

Other inclusive int bound. For a nonempty array index, the upper bound is Length - 1.

Returns int

int from min(from, to) through max(from, to), including the full int range. Equal bounds return that value without advancing State.

Randomizevoid
# Randomize
C#
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
# SetSeed
C#
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.

Parameters
NameTypeDefault
seedulong—
seed

Initial ulong value. Defines a repeatable start with the same algorithm, parameters and call order.

SetStatevoid
# SetState
C#
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.

Parameters
NameTypeDefault
stateulong—
state

Previously captured ulong state. Preserve its full precision when storing and reading it.

Reckfell Engine documentationBack to top ↑
QUICK LINKSGetting startedEntity hierarchyC# & .NET 10Scenes and prefabs
Reckfell guides and full API K
reckfellENGINE
RECKFELL ACCOUNT

Sign in

Sign in to your Reckfell account.

One account for the entire Reckfell platform.