w>O@D1.
Outputs 0
with 1/3 probability, 1
with 2/3 probability. Try it online!
I think this is the first time I've had to pad the source code with no-ops to make the cube the correct size...
Explanation
Cubix is a 2D stack-based esolang wrapped around a cube. The source code won't fit onto a size-1 cube, and so it gets wrapped around a size-2 cube, with the following cube net:
w >
O @
D 1 . . . . . .
. . . . . . . .
. .
. .
The IP (instruction pointer) is then sent into action, starting at the top-left corner of the left-most face, and facing east. The first instruction it hits is D
, which points the IP in a random direction. This isn't the best possible source of randomness for this challenge, but it's the only one Cubix has.
If the IP is pointed north, it wraps onto the w
on the top face, which moves the IP one position to the right (south at this point). O
outputs the top item on the stack as a number (0
if the stack is empty), and @
ends the program.
If the IP is pointed south, it wraps around various faces before hitting the >
on the top face. It's going west at this point, and >
points the cursor east, sending it back to the D
, which starts the whole process over.
If the IP is pointed east or west, it simply wraps around the third row of the above diagram, hitting the 1
at some point in the middle. When it gets back to the D
, the process is started again as before, but when the IP eventually gets sent north, O
outputs 1
instead of 0
.
So each time D
is hit, there's a 2/4 chance that the output will be 1
, and a 1/4 chance that it will be 0
. The other 1/4 is simply the chance of the process starting over, and so if we sum the infinitely shrinking chances of each output, we get 2/3 for 1
and 1/3 for 0
.
Comments are not for extended discussion; this conversation has been moved to chat.
– Martin Ender – 2017-03-30T17:02:46.3735I don't think the question is actually a duplicate of the one marked. There is a slight variation if I'm not wrong. And the snippet get a random true or false value - and that answers the question! – Keerthana Prabhakaran – 2017-03-30T17:56:11.520
The questuon 1/n probability is a more specific version of this question. Here it's just truths or false. The other one was printing a true in 1/n probability. This is what I meant when I said a difference in precious comment. – Keerthana Prabhakaran – 2017-03-30T18:01:01.627
2Not sure if it still matters, but in case your challenge gets reopened, you should clarify what you mean by it does not have to be uniform. – Dennis – 2017-03-30T18:20:38.997
If it does not have to be uniform, there is a 100% chance for exit code 0 on empty program in lots of languages... – fəˈnɛtɪk – 2017-03-30T18:38:48.437
1
As I said in the chat room, if this challenge should be closed as a dupe, the vanilla hello, world challenge should be closed as a dupe of the similar challenges that came before it for all the same reasons.
– Mike Bufardeci – 2017-03-30T19:15:02.677Out of curiosity, would an answer written as a Windows command from the Command Line count as valid (22 chars)? set/a a=%RANDOM%/16384 – Filipus – 2017-03-31T19:47:33.667