Flip a coin for me!

1

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.

Matthew Roh

Posted 2017-03-30T16:06:03.057

Reputation: 5 043

Question was closed 2017-03-30T17:14:00.560

Comments are not for extended discussion; this conversation has been moved to chat.

– Martin Ender – 2017-03-30T17:02:46.373

5I 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.677

Out 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

Answers

7

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.

Dennis

Posted 2017-03-30T16:06:03.057

Reputation: 196 637

2Hah, great trick outputting via exit code. – ETHproductions – 2017-03-30T17:07:55.773

2

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

Luis Mendo

Posted 2017-03-30T16:06:03.057

Reputation: 87 464

2

Pyth, 2 bytes

O2

Outputs 0 for heads, 1 for tails.

Similarity to any real-world company only incidental.

Try it!

KarlKastor

Posted 2017-03-30T16:06:03.057

Reputation: 2 352

2

JavaScript (ES6), 13 bytes

_=>new Date%2

Test snippet

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

ETHproductions

Posted 2017-03-30T16:06:03.057

Reputation: 47 880

2

Powershell, 8 Bytes

random 2

outputs either 0 or 1 randomly.

colsw

Posted 2017-03-30T16:06:03.057

Reputation: 3 195

2

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

Brad Gilbert b2gills

Posted 2017-03-30T16:06:03.057

Reputation: 12 713

2

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!

ETHproductions

Posted 2017-03-30T16:06:03.057

Reputation: 47 880

1

CJam, 3 bytes

2mr

Try it online!

Explanation

2    e# Push 2
 mr  e# Random integer from 0 to n-1

Business Cat

Posted 2017-03-30T16:06:03.057

Reputation: 8 927

1

PHP, 13 bytes

<?=rand(0,1);

Outputs a pseudo-random 0 or 1 value.

Test online

Mario

Posted 2017-03-30T16:06:03.057

Reputation: 3 043

1

Octave, 10 bytes

@()rand<.5

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

Try it online!

Luis Mendo

Posted 2017-03-30T16:06:03.057

Reputation: 87 464

1

Jelly, 3 bytes

2ḶX

Try it online!

KarlKastor

Posted 2017-03-30T16:06:03.057

Reputation: 2 352

1

><>, 14 11 bytes

x0n;n0
>1n;

Try it online!

Outputs either 0 or 1

user41805

Posted 2017-03-30T16:06:03.057

Reputation: 16 320

1

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.

ETHproductions

Posted 2017-03-30T16:06:03.057

Reputation: 47 880

1

05AB1E, 4 3 bytes

T.R

Try it online!

Explanation:

T   10
 .R Random choice (breaks into individual digits)

Erik the Outgolfer

Posted 2017-03-30T16:06:03.057

Reputation: 38 134

I believe that T.R also works. – Adnan – 2017-03-30T17:07:58.037

@Adnan It does indeed, even though it pushes 10 and not '10' in this case. – Erik the Outgolfer – 2017-03-30T17:11:59.173

1

Röda, 13 bytes

randomBoolean

Try it online!

This is a boring builtin.

fergusq

Posted 2017-03-30T16:06:03.057

Reputation: 4 867

1

*><>, 6 bytes

x0n;n1

Online interpreter

Erik the Outgolfer

Posted 2017-03-30T16:06:03.057

Reputation: 38 134

0

Erik the Outgolfer

Posted 2017-03-30T16:06:03.057

Reputation: 38 134

0

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.

Mike Bufardeci

Posted 2017-03-30T16:06:03.057

Reputation: 1 680