Writing a linear congruential generator (and a Rational class) in Dart

A linear congruential generator (LCG) produces pseudorandom numbers with one recurrence:
x(n+1) = (a * x(n) + c) mod ma is the multiplier, c the increment and m the modulus. The output looks random but is fully
determined by the seed, and it repeats with a period of at most m. LCGs are cheap and easy to
reproduce, which makes them fine for simulations, games and tests. They are not suitable for
cryptography: a few consecutive outputs are enough to recover the parameters and predict the rest.
Dart’s own Random.secure() exists for that.
The code below comes from my dart-basic-math-lib
repo, which also has a Rational class. The repo targets Dart 2 without null safety and doesn’t
compile on Dart 3, so the versions here are updated for Dart 3.
The interface
Section titled “The interface”abstract interface class LcgInterface { int get multiplier; int get increment; int get modulus; int get seed; set seed(int newSeed);
/// Current state, without advancing. int currValue();
/// Advances one step and returns the new state. int nextNum();
/// Advances [n] steps. void discard(int n);
int min(); int max();}The original was a concrete class with private fields and dummy method bodies. An
abstract interface class (Dart 3) states the contract without any of that, and the implementation
below can implement it without inheriting fields it can’t see.
The generator
Section titled “The generator”import 'lcg_interface.dart';
class LCG implements LcgInterface { final int _a; // multiplier final int _c; // increment final int _m; // modulus int _seed; int _xi; // current state
LCG(int multiplier, int increment, int modulus, int seed) : assert(modulus > 0), _a = multiplier, _c = increment, _m = modulus, _seed = seed, _xi = _initialState(increment, modulus, seed);
// Same rule as C++'s std::linear_congruential_engine: if the increment and // the seed are both multiples of the modulus, start at 1 instead of 0. // Otherwise a multiplicative generator (c == 0) would output 0 forever. static int _initialState(int c, int m, int seed) => (c % m == 0 && seed % m == 0) ? 1 : seed % m;
@override int get multiplier => _a; @override int get increment => _c; @override int get modulus => _m; @override int get seed => _seed;
@override set seed(int newSeed) { _seed = newSeed; _xi = _initialState(_c, _m, newSeed); }
@override int currValue() => _xi;
@override int nextNum() => _xi = (_a * _xi + _c) % _m;
@override void discard(int n) { for (var i = 0; i < n; i++) { nextNum(); } }
@override int min() => _c == 0 ? 1 : 0;
@override int max() => _m - 1;
// Two generators are equal if they have the same parameters. @override bool operator ==(Object other) => other is LCG && other._a == _a && other._c == _c && other._m == _m;
@override int get hashCode => Object.hash(_a, _c, _m);}Changes from the original:
==now takesObjectand there is a matchinghashCode. Overriding==with a narrower parameter type (LCG other) is a compile error._xiis set in the initializer list, which null safety requires for a non-nullable field.- The seed setter resets the state. Before, it changed the stored seed but kept generating from the old state.
- The initial state is
seed % mrather thanseed, so a seed larger than the modulus is still a valid state.
Equality only compares the parameters, not the current state, so two generators at different
points in the same sequence are still ==. C++‘s std::linear_congruential_engine compares state
instead; pick whichever matches how you use it.
Checking it
Section titled “Checking it”minstd_rand from the C++ standard library is an LCG with a = 48271, c = 0, m = 2^31 - 1 and a
default seed of 1. The standard requires its 10,000th output to be 399268537, which makes a handy
test:
import 'lcg.dart';
void main() { final minstd = LCG(48271, 0, 2147483647, 1); print(minstd.nextNum()); // 48271 print(minstd.nextNum()); // 182605794
final check = LCG(48271, 0, 2147483647, 1)..discard(9999); print(check.nextNum()); // 399268537}Gotchas
Section titled “Gotchas”- Overflow. On the Dart VM and in AOT builds,
intis 64-bit and wraps silently.a * x + cmust stay below 2^63, soa * (m - 1) + cis the limit to check. On the web,intis a JavaScript number and is only exact up to 2^53, so the same parameters can give different sequences in a browser. - Bad parameters give short periods.
LCG(5, 4, 3, 2)(the example in the repo) outputs2forever. Withc != 0, the period is the fullmonly whencandmare coprime,a - 1is divisible by every prime factor ofm, anda - 1is divisible by 4 ifmis (the Hull–Dobell theorem). Use published constants rather than picking your own. - For anything that isn’t a learning exercise,
dart:math’sRandom(seed)already takes a seed.
The Rational class
Section titled “The Rational class”The same repo has a Rational class that keeps fractions in lowest terms, puts the sign on the
numerator, and stores zero as 0/1, so 2/-8 becomes -1/4 and 0/-128 becomes 0/1. The
original mutated itself in a simplify() method; this version is immutable and simplifies once in
the factory constructor:
class Rational implements Comparable<Rational> { final int numerator; final int denominator; // always > 0
Rational._(this.numerator, this.denominator);
factory Rational(int numerator, int denominator) { if (denominator == 0) { throw ArgumentError.value(denominator, 'denominator', 'must not be zero'); } // int.gcd is never negative; gcd(0, d) == |d|, so zero becomes 0/1. final g = numerator.gcd(denominator); var n = numerator ~/ g; var d = denominator ~/ g; if (d < 0) { n = -n; d = -d; } return Rational._(n, d); }
static int _lcm(int a, int b) => a ~/ a.gcd(b) * b;
Rational operator +(Rational other) { final lcm = _lcm(denominator, other.denominator); return Rational( numerator * (lcm ~/ denominator) + other.numerator * (lcm ~/ other.denominator), lcm, ); }
Rational operator -(Rational other) => this + Rational(-other.numerator, other.denominator);
Rational operator *(Rational other) => Rational(numerator * other.numerator, denominator * other.denominator);
// Throws ArgumentError when dividing by zero. Rational operator /(Rational other) => Rational(numerator * other.denominator, denominator * other.numerator);
// Denominators are positive, so cross-multiplying keeps the order. @override int compareTo(Rational other) => (numerator * other.denominator).compareTo(other.numerator * denominator);
bool operator <(Rational other) => compareTo(other) < 0; bool operator >(Rational other) => compareTo(other) > 0;
@override bool operator ==(Object other) => other is Rational && other.numerator == numerator && other.denominator == denominator;
@override int get hashCode => Object.hash(numerator, denominator);
/// Integer part, rounded toward zero (-7/2 gives -3). int truncate() => numerator ~/ denominator;
bool get isInteger => denominator == 1;
@override String toString() => '$numerator/$denominator';}
void main() { print(Rational(2, -8)); // -1/4 print(Rational(0, -128)); // 0/1 print(Rational(1, 2) + Rational(1, 3)); // 5/6 print(Rational(1, 2) < Rational(2, 3)); // true print(Rational(-7, 2).truncate()); // -3}Behaviour that changed from the original:
- Dividing by zero now throws. The original quietly turned
x/0into9223372036854775807/1. truncate()rounds toward zero. The original used%, which in Dart is never negative, so-7/2truncated to-4.==takesObjectand has ahashCode, for the same reason as in the LCG.
Because simplification is applied on every operation and equal values always have the same
representation, comparing numerator and denominator is enough for ==.
References
Section titled “References”© 2026 David Li · Home · Blog · RSS ·Classic site · Built with Astro, Starlight and three.js · Logos:Font Awesome Free (CC BY 4.0)