Skip to content

SpecificationXII · Library

12.5

Reproducible randomness

PseudoLearn's randomness comes from a source with a seed: with the same seed, the program always draws the same numbers.

Markdown version

12.5.1 How it works

FunctionReturns
random(n)an integer between 0 and n - 1
randomBetween(a, b)an integer between a and b, both included
randomReal()a real between 0 and 1, never reaching 1

All three read the same source, and the source starts from a seed set by the host. That is why a run can be repeated exactly: in the app, in the terminal with --semilla and in this book, which always uses the same one.

Figure 12.5-1.With the same seed, two runs draw the same sequence. With another seed, the sequence changes.

Two runs with the same seed draw the same sequence 1, 1, 6, 15. A run with seed 7 draws a different one.

ProgramThree dice

Algorithm Dados
  Write random(6) + 1, " ", random(6) + 1, " ", random(6) + 1;
  Write randomBetween(10, 20), " ", randomReal() < 1.0;
EndAlgorithm

Output

1 1 6
15 true

The program finishes normally.

In short

  • random, randomBetween and randomReal share a seeded source.
  • The same seed gives the same sequence.
  • Invalid bounds stop the program.