aboutsummaryrefslogtreecommitdiff
path: root/rand/README.md
diff options
context:
space:
mode:
authorMiquel Sabaté Solà <mikisabate@gmail.com>2025-05-06 16:06:20 +0200
committerMiquel Sabaté Solà <mikisabate@gmail.com>2025-05-06 16:22:48 +0200
commit111be468f0c78a572731ba1a273f8166dcdf7b1c (patch)
treefc6ebb97f580dafb9f949627d7720b0c86818922 /rand/README.md
parentee183ebbdfdf407044948aefa06f8a131dd23213 (diff)
downloadcode.nes-111be468f0c78a572731ba1a273f8166dcdf7b1c.tar.gz
code.nes-111be468f0c78a572731ba1a273f8166dcdf7b1c.zip
rand: Add an example with PRNG
Signed-off-by: Miquel Sabaté Solà <mikisabate@gmail.com>
Diffstat (limited to 'rand/README.md')
-rw-r--r--rand/README.md33
1 files changed, 33 insertions, 0 deletions
diff --git a/rand/README.md b/rand/README.md
new file mode 100644
index 0000000..12f8902
--- /dev/null
+++ b/rand/README.md
@@ -0,0 +1,33 @@
+## Generating pseudo-random numbers
+
+Usually there is a requirement from NES/Famicom games to have access to random
+numbers. This is not entirely possible on the NES/Famicom since there is no
+hardware-specific implementation for any of this (i.e. as it happens on modern
+CPUs). But there's this common saying: if you can't make it, fake it! That's a
+common mentality when programming on the NES/Famicom due to its shortcomings in
+comparison to modern hardware.
+
+In this case, there are quite clever algorithms for generating **pseudo** random
+numbers. That is, numbers which are not absolutely random, but they are quite
+close to it.
+
+First of all, you need to generate a **seed**: from which number you start
+generating new random numbers. The approach taken here is the same as in many
+other games: the player is presented with a `Start` screen. On `nmi` code we
+count the frames until the player actually presses the `Start` button.
+Obviously, this is not really random, but unless you are on a TAS or you are an
+amazing player with frame-perfect input skills, it's good enough.
+
+All of this is implemented on the [rand.s](./rand.s) file. After the "Start"
+screen the player is presented with the algorithm being used and the random
+value that we got. The player can then press `Select` to change the algorithm
+being used, or press `A` to select a new number. There are a total of two PRNG
+algorithms being tested here:
+
+- [linear.s](./linear.s): A Galois linear feedback shift register (16-bit).
+- [precalc.s](./precalc.s): Indexing a pre-computed set of random numbers.
+
+These algorithms are better explained at the top comment from their respective
+files. All in all, we get the following result:
+
+![rand.gif](../docs/rand.gif)