liveBook · Manning

Pseudo-random number generator

As explained in Functional Programming in Scala, Second Edition

Pseudo-random number generation is the deterministic production of values that approximate randomness from an initial seed.

A pseudo-random number generator deterministically produces values that approximate randomness from an initial seed. The central design tension is whether generation mutates hidden state or returns the next state explicitly. section 6.0 section 6.8

Two interfaces expose the difference between effectful and purely functional generation:

  • scala.util.Random stores and updates internal state; repeated calls consume that state, making failures difficult to reproduce unless both the seed and call history match. section 6.1
  • A functional RNG returns both the generated value and a new RNG, leaving the previous state unchanged and giving the caller control over state progression. section 6.2
  • The pattern generalizes as Rand[A], a function from Rng to a value and updated Rng, with combinators such as unit, map, map2, flatMap, and sequence. section 6.8

A deterministic functional sequence

Starting with SimpleRNG(42), the first call returns 16159453 and a new RNG; calling that returned RNG produces -1281479697 and another new RNG. Repeating the same sequence reproduces the same values. section 6.2

Listing 6.1 Using scala.util.Random to generate random numbers
scala> val rng = new scala.util.Random   #1
 
scala> rng.nextDouble
val res1: Double = 0.9867076608154569
 
scala> rng.nextDouble
val res2: Double = 0.8455696498024141
 
scala> rng.nextInt
val res3: Int = -623297295
 
scala> rng.nextInt(10)                   #2
val res4: Int = 4

Compare successive calls to scala.util.Random, including bounded integer generation, before replacing hidden mutation with explicit state. section 6.1

How Functional Programming in Scala, Second Edition covers it

How deep it goes
Explains mutation, reproducibility, explicit state transitions, and compositional Rand abstractions, while deferring broader stateful effects.
Examples
Uses scala.util.Random, die rolling, SimpleRNG(42), explicit seed threading, and reproducible generated integers.
What it assumes
Assumes basic Scala programming while introducing the functional treatment of state from a simple random-generation problem.
What you can do after
Build it from scratch as a pure seed-to-value-and-state function and compose it with Rand operations.
How it connects
Connects pseudo-random generation to side effects, pure functions, referential transparency, Rand, and State.

Also explained in

Compare how 10 books explain pseudo-random number generator →

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.