Random Brainfuck
Brainfuck plus one question mark, which puts a random byte in the cell.
2015 · Turing complete: yes · ●○○ easy · esolangs.org
Tested with 3 known programs · Hello World on Try It Online: identical · Test method
Run it
The idea
Brainfuck always does the same thing on the same input. Random Brainfuck adds ?, which overwrites the current cell with a random number from 0 to 255, all equally likely. That is enough for dice, coin flips and simple games. Anything narrower, like a die from 1 to 6, takes a remainder with the usual brainfuck division loops.
The machine
A tape of byte cells that wrap at 256, growing both ways, and a pointer.
| Command | Does |
|---|---|
+ - < > . , [ ] | As in brainfuck |
? | Set the cell to a random byte |
The random bytes come from a fixed seed (xorshift32), so a program draws the same bytes every time it runs and every result can be replayed. On TIO each run draws new ones. Reading past the end of the input gives 0, and every other character is a comment.
Example
TIO's hello world starts with +[?], which keeps drawing until it gets a 0, and then prints the same text…
TIO's hello world starts with +[?], which keeps drawing until it gets a 0, and then prints the same text whatever was drawn. The second program is the random capital letter, a random byte modulo 26 plus
- With this seed the first byte is 43, and 43 modulo 26 is 17, so it prints
R.
Patterns it teaches
- Nondeterminism: one command makes a program's output a distribution instead of a single text.
See also
Random Brainfuck builds on Brainfuck.
Brainfuck derivatives
Write your own in Run Convert a program from Random Brainfuck
Implementation
JavaScript interpreter, source and command line
A JavaScript interpreter, used in the browser and on the command line. Id: random-brainfuck.
Command line
shib run random-brainfuck -e ',.?,.' --input 'ab'shib manual random-brainfuckSource
- Module
langs/tio/ports.js- Interpreter
langs/tio/random-brainfuck.js
Docs: Kernel instructions · The .shib format · Command line · Adding a language
Source esolangs.org (CC0) · Edit on esolangs.org