# Random

> Random numbers you can reproduce: one seeded generator for the whole game, with the handful of shapes a game actually asks for.

Source: https://omardev29.github.io/rmp-docs/manual/modules/random.html

[`rmp::random`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html) is one generator of random numbers for the whole game, and the sequence it gives depends on one number, its seed. Every launch gets a different seed, so the game varies; the same seed gives the same numbers in the same order, so a run can be repeated — for a roguelike's shareable seed, a bug report, a replay, or a test that gives the same answer every time.

## The numbers

```cpp
const float angle = rmp::random::range(0.0f, 360.0f);
const int damage = rmp::random::range(3, 6);         // 3, 4, 5 or 6
const bool critical = rmp::random::chance(0.25f);    // one time in four
const Vector2 spark = rmp::random::in_circle(80);    // a point within 80 units of the centre
const Vector2 heading = rmp::random::direction();    // a unit vector, any angle
```

| Call | Gives |
|---|---|
| [`value()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#value) | A float from 0 up to, never reaching, 1. |
| [`range(min, max)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#range) with floats | A float from `min` up to, never reaching, `max`. |
| `range(min, max)` with ints | An int from `min` to `max`, *both included*, as raylib's `GetRandomValue()` counts. When `max` is not above `min`, `min` — where `GetRandomValue()` swaps the two, so `range(6, 3)` is always 6 and `GetRandomValue(6, 3)` is 3 to 6. |
| [`chance(p)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#chance) | `true` with probability `p`: 0 is never, 1 is always. |
| [`in_circle(radius)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#in_circle) | A point spread evenly over the whole disc, not bunched at the middle and not only on the rim. |
| [`direction()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#direction) | A vector of length 1, pointing anywhere. |
| [`index(count)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#index) | An index from 0 to `count - 1`; 0 for a count of 0 or less, so it needs no check before it is used. |

The two `range()`s are told apart by their arguments, so both have to be the same type. A float and an int together is a question with two answers, and the compiler says so rather than picking one:

Does not compile

```cpp
const float x = rmp::random::range(0, 1.5f);
```

The compiler refuses it, saying among other things: `is ambiguous`

Write `range(0.0f, 1.5f)`.

### Picking and shuffling

`index()` picks one of a list:

```cpp
const std::vector<std::string> greetings = { "Hello!", "Nice day.", "Watch out." };
const std::string &line = greetings[rmp::random::index(static_cast<int>(greetings.size()))];
```

and shuffles one, from the last element down, each swapped with one at or before it:

```cpp
std::vector<int> bag = { 0, 1, 2, 3, 4, 5, 6 };
for (int i = static_cast<int>(bag.size()) - 1; i > 0; --i) {
    std::swap(bag[i], bag[rmp::random::index(i + 1)]);
}
```

That is the falling-block game's bag in [examples/games/06_tetris](https://omardev29.github.io/rmp-docs/examples/games/06_tetris.html): every seven pieces are the seven pieces in a shuffled order, which is fairer than one at random each time, and with the same seed it is the same game ([main.cpp](https://github.com/omardev29/raylib_multiplatform/blob/982bf2a1e307c49776bff84211260703b8de6895/examples/games/06_tetris/src/main.cpp#L221-L233)). [examples/games/03_space_invaders](https://omardev29.github.io/rmp-docs/examples/games/03_space_invaders.html) gives every alien a shooting period of its own with `range(4.0f, 9.0f)`, so that a whole formation started on the same frame does not fire as one gun.

## The seed

The framework seeds the generator once, when the game starts, from the clock, so a game that never mentions seeds plays differently each time it is launched. [`current_seed()`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#current_seed) is the seed the sequence running now started from — the number to print in a bug report or show on a roguelike's pause screen:

```cpp
TraceLog(LOG_INFO, "seed %llu",
         static_cast<unsigned long long>(rmp::random::current_seed()));
```

[`seed(n)`](https://omardev29.github.io/rmp-docs/reference/namespaces/rmp-random.html#seed) starts the sequence again from `n`. Called with the number a player typed, or the one from the bug report, it gives the same numbers in the same order:

```cpp
class RunScene : public rmp::Scene {
public:
    explicit RunScene(uint64_t seed) : _seed(seed) {}

private:
    void _ready() override {
        rmp::random::seed(_seed); // the same seed, the same dungeon
    }

    uint64_t _seed;
};
```

`current_seed()` is always the truth, even before anything has called `seed()`: a run nobody seeded starts from a fixed default seed, and reports that one.

### What "the same run" takes

The seed fixes the sequence of numbers; it does not fix who asks for them. There is one generator for the whole game — the behaviors use it too: a [`Spawner`](https://omardev29.github.io/rmp-docs/reference/behaviors/Spawner.html) with jitter draws each interval's jitter from it — so a run repeats when the same things ask for numbers in the same order. Anything that differs from one run to the next — what the player pressed, and when — changes which number goes where. Seeding at the start of a level and generating the level at once, before any of that happens, is what makes a seeded level the same level every time.

### Tests are reproducible on their own

The seed from the clock is set by the entry point, and a unit test does not go through the entry point. So a test of anything that varies — a spawner's jitter, a wander, a shake — starts from the same default seed every time it runs and gives the same answer, without seeding anything. A test that fails one time in twenty teaches everyone to ignore a red test.

A test that plays a whole game does go through the entry point, and names the seed it starts from instead of the clock: that is how the framework's own tests [play an example from a script](https://omardev29.github.io/rmp-docs/framework/testing.html#e2e), and why the falling-block game deals the same pieces every time they do. The seed is named through a seam inside the framework, not through `rmp::random`.

## Why not rand()

The C library's `rand()` keeps its state in one global that every library in the process shares, so any of them calling `srand()` changes your sequence, and the low bits of what it returns are poor. raylib's `GetRandomValue()` keeps its state inside raylib and seeds it from the clock. `rmp::random` is a generator of the framework's own, so the game and its tests decide where a sequence starts. It is xoshiro128++: sixteen bytes of state, no allocation, and good enough for a game by a wide margin.

Not for secretsA sequence you can reproduce from one number is a sequence someone else can predict. Nothing that has to be unguessable — a token, a password, shuffling cards for real money — belongs on `rmp::random`.
