SeededRandom class
Deterministic, cross-platform-stable pseudo-random generator —
implements Random so it's a drop-in source for weightedRandomPick,
List.shuffle, or anywhere else a dart:math Random is accepted.
Unlike Random(seed), this:
- Exposes its internal state via snapshot/SeededRandom.fromSnapshot
so a caller can persist and later resume a sequence exactly — plain
dart:mathRandomhas no such API at all. - Uses hand-rolled 32-bit-safe integer arithmetic (a mulberry32 variant)
instead of relying on native 64-bit int overflow, so the SAME seed +
call sequence produces the SAME output on every Dart compile target
(the Dart VM, where
intis a real 64-bit integer, AND a JS web build, where numbers above 2^53 silently lose precision — a naive 64-bit generator would drift between the two).
- Implemented types
Constructors
- SeededRandom(int seed)
-
factory
- SeededRandom.fromSnapshot(RandomSnapshot snapshot)
-
factory
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
nextBool(
) → bool -
Generates a random boolean value.
override
-
nextDouble(
) → double -
Generates a non-negative random floating point value uniformly distributed
in the range from 0.0, inclusive, to 1.0, exclusive.
override
-
nextInt(
int max) → int -
maxmust be in1..0x100000000(a single 32-bit draw's full range) — this generator doesn't support a wider range in one call.override -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
pick<
T> (List< T> items, List<double> weights) → T -
Weighted pick, bridging straight to the existing weightedRandomPick
— same validation/selection algorithm, sourced from this deterministic
generator instead of
dart:math's ownRandom. Invalid input (empty list, mismatched lengths, a bad weight) throws before this generator's state is touched, same as weightedRandomPick itself. -
shuffle<
T> (List< T> items) → void -
Shuffles
itemsin place — a thin, discoverable wrapper over the built-inList.shuffle(Random), sinceSeededRandomalready implements Random. -
snapshot(
) → RandomSnapshot -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Constants
- algorithmVersion → const int
- Bumped only if the generator algorithm itself ever changes — lets fromSnapshot reject a snapshot produced by an incompatible version instead of silently producing a different (wrong) sequence from it.