Random Brainfuck

From Esolang
Jump to: navigation, search

Random Brainfuck is an extension to Brainfuck. It supports all the operations that Brainfuck supports, but has one extra operation i.e. the question mark (?) which overwrites the current cell with a random number. This adds a grain of unpredictability to Brainfuck, which makes it a better language for creating simple games.

Language overview

Command Description
> Move one cell to the right
< Move one cell to the left
+ Increment the current cell by one
- Decrement the current cell by one
[ If the current cell is zero, jump program execution to the corresponding ]
] If the current cell is non-zero, jump program execution to the corresponding [
. Output the character in the current cell
, Read a character and store it in the current cell
? Generate a random byte and store it in the current cell

As with Brainfuck, all other characters are completely ignored and can be used to comment the program.

Implementation

The random number generator should be able to generate the numbers 0 (inclusive) to 255 (inclusive) with uniform probability. The generated number should either be truly random or be generated using a pseudorandom generator. However if an implementation does the latter, it should at least be seeded.

Java equivalent

Initialisation of the random number generator before interpreting the RB Program:

randomGenerator = Random();

Invocation of ? during execution of the RB program.

cell[ptr] = randomGenerator.nextInt(256)

Examples

Die from 1 to 6

Usually a number is wanted in a domain different from 0-255. To get a number in a different domain, it's often useful to perform a modulo operation.

; n = random byte
?
; cell 0| n

; Use the divmod golf trick
; Now we have a number from 1 to 6 in cell 1
>++++++<[->-[>+>]>[+[-<+>]>>]<<<<]#
; cell 0| 0
; cell 1| n minus (n mod 6)
; cell 2| n mod 6

; Add 48 to cell 1 for converting it to a decimal
++++++[->++++++++<]>

; Print the result
.

Infinite random binary data

Prints random binary data, never terminates.

+[>?.<]