<>, Invalid =============
I'll try thinking of a way to do 1/N probability in ><>, in the meantime I thought you might find a 1/2^N answer interesting.
4{:?!v1-}:">"$2p:"x"$3p:"^"$4p1+:">"$3p1+!
^1<
0n;
1n;>
Input is to be input directly on the stack with -v to avoid the tedious integer parsing. Truthey output will be 1, falsey will be 0. So for an input 3 you've got 1/8 chances of getting 1 and 7/8 of getting 0.
Explanation :
I append as much x
as needed on the 4th line and surround them with directions so there is only two ways out of the x
: either the falsey output or the next x
. If all x
go in the right direction, the last one will route to the truthy output.
For example for N=5, the final codespace is the following :
4{:?!v1-}:">"$2p:"x"$3p:"^"$4p1+:">"$3p1+!
^1<
0n; > > > > >
1n;>x>x>x>x>x>
^ ^ ^ ^ ^