Skip to content

Commit a9ab1a0

Browse files
committed
use rfc text for overview on bevy_entropy
1 parent 3bcdbc2 commit a9ab1a0

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

crates/bevy_entropy/README.md

+24-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ Bevy Entropy is a plugin to provide integration of `rand` ecosystem PRNGs in an
1212

1313
For a PRNG crate to be usable with Bevy Entropy, at its minimum, it must implement `RngCore` and `SeedableRng` traits from `rand_core`, as well as `PartialEq`, `Clone`, and `Debug` traits. For reflection/serialization support, it should also implement `Serialize`/`Deserialize` traits from `serde`, though this can be disabled if one is not making use of reflection/serialization. As long as these traits are implemented, the PRNG can just be plugged in without an issue.
1414

15+
## Overview
16+
17+
Games often use randomness as a core mechanic. For example, card games generate a random deck for each game and killing monsters in an RPG often rewards players with a random item. While randomness makes games more interesting and increases replayability, it also makes games harder to test and prevents advanced techniques such as [deterministic lockstep](https://gafferongames.com/post/deterministic_lockstep/).
18+
19+
Let's pretend you are creating a poker game where a human player can play against the computer. The computer's poker logic is very simple: when the computer has a good hand, it bets all of its money. To make sure the behavior works, you write a test to first check the computer's hand and if it is good confirm that all its money is bet. If the test passes does it ensure the computer behaves as intended? Sadly, no.
20+
21+
Because the deck is randomly shuffled for each game (without doing so the player would already know the card order from the previous game), it is not guaranteed that the computer player gets a good hand and thus the betting logic goes unchecked.
22+
While there are ways around this (a fake deck that is not shuffled, running the test many times to increase confidence, breaking the logic into units and testing those) it would be very helpful to have randomness as well as a way to make it _less_ random.
23+
24+
Luckily, when a computer needs a random number it doesn't use real randomness and instead uses a [pseudorandom number generator](https://en.wikipedia.org/wiki/Pseudorandom_number_generator). Popular Rust libraries containing pseudorandom number generators are [`rand`](https://crates.io/crates/rand) and [`fastrand`](https://crates.io/crates/fastrand).
25+
26+
Pseudorandom number generators require a source of [entropy](https://en.wikipedia.org/wiki/Entropy) called a [random seed](https://en.wikipedia.org/wiki/Random_seed). The random seed is used as input to generate numbers that _appear_ random but are instead in a specific and deterministic order. For the same random seed, a pseudorandom number generator always returns the same numbers in the same order.
27+
28+
For example, let's say you seed a pseudorandom number generator with `1234`.
29+
You then ask for a random number between `10` and `99` and the pseudorandom number generator returns `12`.
30+
If you run the program again with the same seed (`1234`) and ask for another random number between `1` and `99`, you will again get `12`.
31+
If you then change the seed to `4567` and run the program, more than likely the result will not be `12` and will instead be a different number.
32+
If you run the program again with the `4567` seed, you should see the same number from the previous `4567`-seeded run.
33+
34+
There are many types of pseudorandom number generators each with their own strengths and weaknesses. Because of this, Bevy does not include a pseudorandom number generator. Instead, the `bevy_entropy` plugin includes a source of entropy to use as a random seed for your chosen pseudorandom number generator.
35+
36+
Note that Bevy currently has [other sources of non-determinism](https://github.com/bevyengine/bevy/discussions/2480) unrelated to pseudorandom number generators.
37+
1538
## Concepts
1639

1740
Bevy Entropy operates around a global entropy source provided as a resource, and then entropy components that can then be attached to entities. The use of resources/components allow the ECS to schedule systems appropriately so to make it easier to achieve determinism.
@@ -28,7 +51,7 @@ Bevy Entropy operates around a global entropy source provided as a resource, and
2851

2952
### Forking
3053

31-
If cloning creates a second instance that shares the same state as the original, forking derives a new state from the original, leaving the original 'changed' and the new instance with a randomised seed. Forking RNG instances from a global source is a way to ensure that one seed produces many random yet deterministic states, making it difficult to predict outputs from many sources and also ensuring no one source shares the same state either with the original or with each other.
54+
If cloning creates a second instance that shares the same state as the original, forking derives a new state from the original, leaving the original 'changed' and the new instance with a randomised seed. Forking RNG instances from a global source is a way to ensure that one seed produces many deterministic states, while making it difficult to predict outputs from many sources and also ensuring no one source shares the same state either with the original or with each other.
3255

3356
Bevy Entropy approaches forking via `From` implementations of the various component/resource types, making it straightforward to use.
3457

0 commit comments

Comments
 (0)