# 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.

Web page: https://pseudolearn.app/en/spec/biblioteca/azar

> **In one sentence**
>
> PseudoLearn's randomness comes from a source with a **seed**: with the same seed, the program always draws the same numbers.

## 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.

**Figure 12.5-1: Same seed, same sequence**

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

With the same seed, two runs draw the same sequence. With another seed, the sequence changes.

### Program: Three dice

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

**Output**

```text
1 1 6
15 true
```

*The program finishes normally.*

> **Limit**
>
> `random(n)` requires `n` greater than zero (`invalidRandomBound`) and `randomBetween(a, b)` requires `a` less than or equal to `b` (`invalidRandomRange`).

**In short**
- `random`, `randomBetween` and `randomReal` share a seeded source.
- The same seed gives the same sequence.
- Invalid bounds stop the program.

**See also**
- [1.4 Determinism: the same program always gives the same result](https://pseudolearn.app/en/spec/vision/determinismo.md)
- [6.4 The Repeat loop](https://pseudolearn.app/en/spec/control/repetir.md)
