liveBook · Manning

Linear congruential generator

As explained in Clojure: The Essential Reference

A linear congruential generator is a pseudorandom number algorithm that repeatedly applies a linear recurrence involving multiplication, addition or seeding, and modular reduction.

A linear congruential generator (LCG) produces pseudorandom values by repeatedly applying multiplication, addition or seeding, and modular reduction. Java and Clojure use an LCG: an initial seed starts a finite sequence that eventually repeats, defining its period. section 5.10

LCG output is practical for ordinary randomness, such as rolling out a feature to roughly half of requests or selecting a branch in an interactive questionnaire. It is not equally random across all bits and can form visible patterns, so it falls short of demanding scientific simulations. section 5.10

Probabilistic rollout

A helper can choose between two outcomes with `(if (< prob (rand)) A B)`. Passing `0.5` makes the branch suitable for an approximate half-and-half rollout, such as showing progress information to some questionnaire users.

How Clojure: The Essential Reference covers it

How deep it goes
Explains seeding, multiplication, modular reduction, repetition, and statistical weaknesses without deriving the full recurrence.
Examples
Uses `rand`, `rand-int`, probabilistic questionnaire branching, and visualized random bits.
What you can do after
Apply Clojure's `rand` and `rand-int` for practical random choices while recognizing when LCG quality is insufficient.
How it connects
Connects pseudorandom number generation with modular arithmetic, Java, Clojure, and scientific simulation.

Also explained in

Compare how 2 books explain linear congruential generator →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Clojure: The Essential Reference. The section links point to the parts of the book it draws on.