12.5.1 How it works
| Function | Returns |
|---|---|
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.
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;
EndAlgorithmOutput
1 1 6 15 true
The program finishes normally.
In short
random,randomBetweenandrandomRealshare a seeded source.- The same seed gives the same sequence.
- Invalid bounds stop the program.