liveBook · Manning

RNG

As explained in Functional Programming in Scala, Second Edition

RNG is a Scala trait interface representing a random number generator whose operation returns a generated integer together with a successor generator state.

RNG is a Scala trait for pure random number generation. Its nextInt method returns both a generated Int and a successor RNG, making state transitions explicit instead of mutating hidden memory. section 6.2

A caller retains control of the successor state while the generator implementation keeps its representation encapsulated. Reusing the same initial RNG therefore produces the same sequence, preserving referential transparency. section 6.2

Threading the successor generator

With SimpleRNG(42), the first call returns 16159453 and a new generator. Calling nextInt on that successor returns -1281479697. Passing the original generator again repeats the first result.

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Defines an interface, implements a linear congruential generator, and develops combinators for composing generated values.
Examples
Uses SimpleRNG(42), deterministic integer outputs, bounded doubles, mixed tuples, triples, and recursively generated integer lists.
What you can do after
Build pure random computations in Scala by returning generated values together with successor generators and composing those pairs.
How it connects
Connects referential transparency, immutable state, Rand, State, map, map2, flatMap, and sequence.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Functional Programming in Scala, Second Edition. The section links point to the parts of the book it draws on.