><>, 25 22 19 bytes
i_ixio;o
ox</;
;\$o
Try it online!, or watch it at the fish playground!
A brief overview of ><>: it's a 2D language with a fish that swims through the code, executing instructions as it goes. If it reaches the edge of the code, it wraps to the other side. The fish starts in the top left corner, moving right. Randomness is tricky in ><>: the only random instruction is x
, which sets the fish's direction randomly out of up, down, left and right (with equal probability).
At the start of the program, the fish reads in two characters of input with i_i
(each i
reads a character from STDIN to the stack, and _
is a horizontal mirror, which the fish ignores now). It then reaches an x
.
If the x
sends the fish rightwards, it reads in one more character (the third), prints it with o
and halts with ;
. The left direction is similar: the fish reads two more characters (so we're up to the fourth), wraps around to the right, prints the fourth character and halts. If the fish swims up, it wraps and prints the second character, before being reflected right by /
and halting. If it swims down, it gets reflected left by the /
and hits another x
.
This time, two directions just send the fish back to the x
(right with an arrow, <
, and up with a mirror, _
). The fish therefore has 1/2 chance of escaping this x
in each of the other two directions. Leftwards prints the top character on the stack, which is the second one, but downwards first swaps the two elements on the stack with $
, so this direction prints the first character.
In summary, the third and fourth characters are printed with probability 1/4 each; the first character has probability 1/2 x 1/4 = 1/8; and the second character has probability 1/4 + 1/2 x 1/4 = 3/8.
ceil(abs(i - 6)/ 2.0)
will map an index from0-7
to an index from0-3
with the appropriate distribution (0 111 22 33
) for this challenge... \$\endgroup\$