2

I'm looking for the code to assign a pseudo-random number to a memory cell in the esoteric language brainf***. I found this sample code, but found it somewhat confusing. From what I could tell it was a "some assembly required" (No pun intended?) sample. Running it resulted in a near-infinite loop. I've also already looked at this question and the Wikipedia article and was still left somewhat confused.

I'm looking for a simple snippet that I can run. I don't care if it affects the cells around it. I only ask for the sample to be well commented.

Community
  • 1
  • 1
rootmeanclaire
  • 808
  • 3
  • 13
  • 37

2 Answers2

1

I had used the algorithm on that page you mentioned to implement an RNG for a brainfuck compiler I was writing. The code there isn't too hard to use. Take all variable names in there (like temp0, temp1, randomh, randoml, etc.) and give them a number. This will be the cell number for that variable. You then simple need to insert the necessary > and < symbols for the variables. Here is an example of the first part. temp0 to temp5 have been given cell numbers 0 to 5, randomh is 6, randoml is 7:

pointer starts at 0

pointer is at 0 and need to go to temp0 (cell 0) so do nothing
temp0         [-]
pointer is at 0 and need to go to temp1 (at cell 1) so move one position to the right
temp1        >[-]
pointer is at 1 and need to go to temp2 (at cell 2) so move another position to the right
temp2        >[-]
pointer at 2 and needs to go to 3 so move another position to the right
temp3        >[-]
pointer at 3 and needs to go to 4 so move another position to the right
temp4        >[-]
pointer at 4 and needs to go to 5 so move another position to the right
temp5        >[-]

pointer at 5 and needs to go to 6 so move one to the right
randomh   >[
    pointer at 6 and needs to go to 0 so move 6 to the left
    temp0    <<<<<<+
    pointer at 0 and needs to go to 6 so move 6 to the right
    randomh  >>>>>>-]

And you keep doing this for each variable in the code. What's important with this algorithm is that the cells assigned to randomh and randoml are not touched by any other code as they contain the random number seed (which will change every time you run this code)

Cedric Mamo
  • 1,724
  • 2
  • 18
  • 33
0

This code sample that came with the BrainfuckMachine IDE works wellf for me.

>>>++[
    <++++++++[
        <[<++>-]>>[>>]+>>+[
            -[->>+<<<[<[<<]<+>]>[>[>>]]]
            <[>>[-]]>[>[-<<]>[<+<]]+<<
        ]<[>+<-]>>-
    ]<.[-]>>
]
"Random" byte generator using the Rule 30 automaton.
Doesn't terminate; you will have to kill it.
To get x bytes you need 32x+4 cells.
rootmeanclaire
  • 808
  • 3
  • 13
  • 37