2
\$\begingroup\$

Note: There was not a vanilla challenge to actually flip a coin, even though there were mentions about flipping coins a lot of places. Even though there is a challenge to generate a random number from 1 to N, I suspect this is not a dupe. These questions ask for random numbers from 1 to N, while this ask for just truthy/falsy.

Flipping a coin is widely used in selecting decisions. But, I do not have a coin (only bills and credit/debit cards).

So, your program has to flip a coin for me!


I/O

Input:

None.

Output:

A truthy/falsy value depending on the coin's state(heads/tails). It does not have to be uniform, seeding by timestamp is allowed. Also, we do not count neither-heads-nor-tails, and assume the coin only has two sides.

\$\endgroup\$
7
  • \$\begingroup\$ Comments are not for extended discussion; this conversation has been moved to chat. \$\endgroup\$ Commented Mar 30, 2017 at 17:02
  • 5
    \$\begingroup\$ I 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! \$\endgroup\$ Commented Mar 30, 2017 at 17:56
  • \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 30, 2017 at 18:01
  • 2
    \$\begingroup\$ Not 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. \$\endgroup\$
    – Dennis
    Commented Mar 30, 2017 at 18:20
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Commented Mar 30, 2017 at 19:15

18 Answers 18

7
\$\begingroup\$

Python 2, 18 17 15 14 bytes

0/(id(0)&4096)

Try it online! Output is via exit code, so check the Debug drawer.

If 50/50 chance isn't required, these shorter versions will work as well.

0/(id(0)%3)   # 11 bytes, awfully biased
0/(id(0)>>25) # 13 bytes, even more biased
0/(id(0)%6<3) # 13 bytes, fair enough for most practical purposes

How it works

In CPython 2.7.12, id(1) yields the memory location of the constant 1 as a 32-bit integer with the following pattern.1

000000yxxxxxxxxxxxxx000110100000

y isn't constant, but it's biased towards 0. All x bit seem to occur with the same probability. I ran the program 100,000 times and these were the distributions.

Bit  Zeroes  Ones
  0  100000       0
  1  100000       0
  2  100000       0
  3  100000       0
  4  100000       0
  5       0  100000
  6  100000       0
  7       0  100000
  8       0  100000
  9  100000       0
 10  100000       0
 11  100000       0
 12   50125   49875
 13   50319   49681
 14   50001   49999
 15   50164   49836
 16   50095   49905
 17   50072   49928
 18   49780   50220
 19   50056   49944
 20   49910   50090
 21   50038   49962
 22   50103   49897
 23   49811   50189
 24   50106   49894
 25   80950   19050
 26  100000       0
 27  100000       0
 28  100000       0
 29  100000       0
 30  100000       0
 31  100000       0

If the twelfth least significant bit is set, id(0)&4096 yields 1 and the program exits with status code 1. However, if it is not set, id(0)&4096 yields 0, a ZeroDivisionError is raised, and the program exits with status code 1.


1 I've verified anything I couldn't find in the docs empirically. Other implementations may behave differently.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ Hah, great trick outputting via exit code. \$\endgroup\$ Commented Mar 30, 2017 at 17:07
2
\$\begingroup\$

MATL, 3 bytes

rEk

This outputs 0 or 1 with the same probability.

Try it online!

Explanation

r     % Random number uniformly distributed on the interval (0,1)
E     % Multiply by 2
k     % Round down
\$\endgroup\$
2
\$\begingroup\$

Pyth, 2 bytes

O2

Outputs 0 for heads, 1 for tails.

Similarity to any real-world company only incidental.

Try it!

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 13 bytes

_=>new Date%2

Test snippet

let f =
_=>new Date%2
<button onclick="console.log(f())">Flip</button>

\$\endgroup\$
2
\$\begingroup\$

Powershell, 8 Bytes

random 2

outputs either 0 or 1 randomly.

\$\endgroup\$
2
\$\begingroup\$

Perl 6, 11 bytes

{Bool.pick}

Try it

Explanation:

  • Bool is an enum of True and False
  • .pick picks an element at random from a list
  • {…} in this case makes it a lambda
\$\endgroup\$
2
\$\begingroup\$

Japt, 3 bytes

K%2

Try it online! (Output cache disabled for obvious reasons)

Outputs 0 or 1 with equal probability. K is equivalent to new Date() in JavaScript, and %2 takes the timestamp mod 2.


Alternatively:

½>Mr

This one generates a random float x between 0 and 1, and outputs .5>x. Try it online!

\$\endgroup\$
1
\$\begingroup\$

CJam, 3 bytes

2mr

Try it online!

Explanation

2    e# Push 2
 mr  e# Random integer from 0 to n-1
\$\endgroup\$
1
\$\begingroup\$

PHP, 13 bytes

<?=rand(0,1);

Outputs a pseudo-random 0 or 1 value.

Test online

\$\endgroup\$
1
\$\begingroup\$

Octave, 10 bytes

@()rand<.5

Anonymous fuction that takes no inputs and returns 0 or 1.

Try it online!

\$\endgroup\$
1
\$\begingroup\$

Jelly, 3 bytes

2ḶX

Try it online!

\$\endgroup\$
1
\$\begingroup\$

><>, 14 11 bytes

x0n;n0
>1n;

Try it online!

Outputs either 0 or 1

\$\endgroup\$
1
\$\begingroup\$

Röda, 13 bytes

randomBoolean

Try it online!

This is a boring builtin.

\$\endgroup\$
1
\$\begingroup\$

*><>, 6 bytes

x0n;n1

Online interpreter

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 4 3 bytes

T.R

Try it online!

Explanation:

T   10
 .R Random choice (breaks into individual digits)
\$\endgroup\$
2
  • \$\begingroup\$ I believe that T.R also works. \$\endgroup\$
    – Adnan
    Commented Mar 30, 2017 at 17:07
  • \$\begingroup\$ @Adnan It does indeed, even though it pushes 10 and not '10' in this case. \$\endgroup\$ Commented Mar 30, 2017 at 17:11
1
\$\begingroup\$

Cubix, 7 bytes

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.

\$\endgroup\$
0
\$\begingroup\$

Gol><>, 3 bytes

xh1

Online interpreter

\$\endgroup\$
0
\$\begingroup\$

Befunge-98, 6 bytes

#1?0>q

Try it online!

Exits with exit code 0 or 1.

Explanation

Program flow in Befunge is a bit difficult to follow. The Instruction Pointer (IP) starts on the left, moving towards the right. The first instruction # tells it to ignore the next instruction, so it jumps to ?. This randomly sets the IP moving either up, down, left, or right. If it moves up or down, it "wraps around" and hits the ? again. If it moves right, 0 is pushed onto the stack and then q exits with the topmost value of the stack (0) as the exit code. If the IP moves left, 1 is pushed onto the stack, q is jumped over, > sets the IP moving to the right again, and then q exits the program with the topmost value of the stack (1) as the exit code.

I believe that this code also works in Unefunge-98, Trefunge-98, and possibly all members of the Funge-98 family. I have not tested any of these claims though.

\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.