7
\$\begingroup\$

Russian roulette

It's Russian roulette! The rules are simple. Shoot a revolver with n slots for bullets and one round inside at your head and you might not die! This Question is different from other Russian roulette questions because it takes input to change the chance of exiting with an error.

Task:

Make a program that takes integer n (you can assume that 10<=n<=128) as input and outputs nothing.

but how do I tell if I'm dead?

The program generates a random number x in the range 0 - n inclusive. If x=0 the revolver fires and the program exits with an error (you die). Otherwise the program exits normally.

If your language needs it, the range can be 1-n inclusive.

Standard loopholes forbidden, etc. Upvote answers in fun languages! What I'd really like to see is an answer in a language that doesn't have normal randomness (eg. in Befunge where ? sends you in a random direction instead of giving a random number).

\$\endgroup\$
16
  • \$\begingroup\$ sandboxed \$\endgroup\$
    – Wezl
    May 4, 2020 at 14:38
  • 1
    \$\begingroup\$ If our language natively generates random number in the range of 1 - n inclusive, instead, is that alright? \$\endgroup\$
    – AviFS
    May 4, 2020 at 15:03
  • 2
    \$\begingroup\$ Related: Make your code error, but only sometimes! \$\endgroup\$
    – corvus_192
    May 4, 2020 at 16:07
  • 4
    \$\begingroup\$ Possible duplicate of Russian Roulette \$\endgroup\$ May 4, 2020 at 17:09
  • 1
    \$\begingroup\$ Re-reading the question, I've realised that several of the current answers (mine included) don't meet the spec. My revolver fires when the random number \$x=0\$, not when \$x=n\$. The probability of firing is the same either way, but can you clarify whether firing when \$x=0\$ (or any value other than \$n\$) is acceptable? \$\endgroup\$
    – Dingus
    May 5, 2020 at 8:08

15 Answers 15

9
\$\begingroup\$

APL (Dyalog Unicode), 3 bytes

Full program. Exits with code 11 or 0. Requires ⎕IO←0.

÷?⎕

Try it online!

 prompt for n

? generate random integer in range 0n-1

÷ reciprocal (errors if argument is 0)

\$\endgroup\$
3
  • 2
    \$\begingroup\$ Darn! Nice one... I've been waiting for a while to post this, but was waiting for OP to relax the requirements. Otherwise, ÷(⊢-?)⎕ is a little closer to the original, though without ⎕IO←0! \$\endgroup\$
    – AviFS
    May 4, 2020 at 15:14
  • 1
    \$\begingroup\$ @AviF.S. Sorry for that. Originally your answer would be eligible but some objected in the sandbox so I changed it... and now I changed it back. \$\endgroup\$
    – Wezl
    May 4, 2020 at 15:18
  • \$\begingroup\$ @Wezl Haha, no worries! Thanks for a challenge simple enough to be accessible to the wildest of esolangs, yet still interesting! Working on an implementation in what you're calling a more 'fun' language! \$\endgroup\$
    – AviFS
    May 4, 2020 at 15:22
4
\$\begingroup\$

JavaScript (V8), 18 bytes

Community consensus is that time can count as a source of randomness. This function will simply take the time in milliseconds modulo the inputted value. If 0, the function tries to return a, which does not exist.

n=>Date.now()%n||a

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Pyth, 3 bytes

JlO

Try it online!

O - generate a random integer in the range 0 - n-1

l - log base 2 of that integer. Throws a math domain error if 0 was generated

J - assign the value to a variable so that we suppress all output

\$\endgroup\$
1
  • \$\begingroup\$ ` ` (space) is the more standard way to suppress printing, but this works too. \$\endgroup\$
    – isaacg
    May 5, 2020 at 8:06
1
\$\begingroup\$

Charcoal, 5 bytes

≔‽‽Nθ

Try it online! Link is to verbose version of code. Explanation: The function reads the input number. The generates a random number between 0 and its argument. However, this errors if the argument is zero. This happens if the inner randomised a 0, i.e. 1 in n times. The ≔θ simply serves to make the program do nothing if it was successful.

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

Rust + time crate, 38 bytes

|n:u8|1/(time::Time::now().second()%n)

Defines a closure that takes an unsigned 8-bit integer as input and returns an integer or panics with "attempt to divide by zero".

try it online.

Our rules about using the time as randomness state the following (emphasis mine):

[...] if you have to select one random value per script execution, current seconds/milliseconds should be sufficient.

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

Python, 57 43 bytes

Saved 14 bytes synchronously with thanks to Surculose Sputum!!!

lambda n:1/randint(0,n)
from random import*

Try it online!

\$\endgroup\$
2
  • 1
    \$\begingroup\$ I don't think you need to output anything, so just lambda n:1/randint(0,n) should work. \$\endgroup\$ May 4, 2020 at 15:42
  • \$\begingroup\$ @SurculoseSputum Was just looking into that - thanks! :-) \$\endgroup\$
    – Noodle9
    May 4, 2020 at 15:46
0
\$\begingroup\$

Python 2, 26 bytes

id(0)%129%-~n gives a random number from 0 to n inclusive. It returns an error if it equals 0, because of ZeroDivisionError.

lambda n:1/(id(0)%129%-~n)

Try it online!

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

Python 2, 23 bytes

1/(id(0)%129%-~input())

which is a merging of dingledooper's answer with my original one (below). Try it online!

Python 2, 40 bytes

from random import*
1/randint(0,input())

Try it online! Full program.

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

MATLAB, 32 bytes

@(n)eval('if(randi(n)==n)a;end')

randi(n) outputs random integer from [1,2,3...n] list

a is undefined, so when it is called, program errors out

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

05AB1E, 5 bytes

Ý<Ω¬F

Try it online!

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

Java (JDK), 22 bytes

n->n/=n*=Math.random()

Try it online!

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

Python3, 66 bytes

0/0 if __import__("random").randint(0,int(input()))==1 else exit()
\$\endgroup\$
2
  • \$\begingroup\$ Welcome to codegolf, and nice first answer! It seems that the double underscore in your code is interpreted as bold mark-up. I suggest you use this online intepreter to format your answer faster - simply click on the "generate perma-link" button (third button at the top), then copy the "Codegolf submission" entry and paste it in your answer here. \$\endgroup\$ May 5, 2020 at 9:19
  • \$\begingroup\$ Ah ok, thanks for the tip! \$\endgroup\$ May 27, 2020 at 10:48
0
\$\begingroup\$

T-SQL, 24 bytes

DECLARE @ INT=10

SET @/=floor(rand()*-~@)

Try it online

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

W j, 2 bytes

g(

Explanation

    Implicit input
g   Randrange 1 to input
 (  Decrement the number by 1

Flag:j (Evaluates the J command at the end of the program)

The J command, given a number operand, reciprocals the number.

It throws a 0-division error when the operand is 0.
\$\endgroup\$
2
  • \$\begingroup\$ If g uses a range from 1 to input, I don't see how can this crash. \$\endgroup\$ May 5, 2020 at 9:58
  • \$\begingroup\$ @mypronounismonicareinstate The range is probably fixed now. \$\endgroup\$
    – user92069
    May 6, 2020 at 0:34
0
\$\begingroup\$

Ruby, 14 bytes

->n{1./rand~n}

Try it online!

Exits with ZeroDivisionError when rand~n returns 0. It's helpful that rand takes the absolute value of its argument.

\$\endgroup\$

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