# 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.

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).

• sandboxed May 4, 2020 at 14:38
• If our language natively generates random number in the range of 1 - n inclusive, instead, is that alright? May 4, 2020 at 15:03
• May 4, 2020 at 16:07
• Possible duplicate of Russian Roulette May 4, 2020 at 17:09
• 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? May 5, 2020 at 8:08

# 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)

• 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! May 4, 2020 at 15:14
• @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. May 4, 2020 at 15:18
• @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! May 4, 2020 at 15:22

# 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!

# 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

•   (space) is the more standard way to suppress printing, but this works too. May 5, 2020 at 8:06

# Charcoal, 5 bytes

≔‽‽Ｎθ


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.

# 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".

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.

# 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!

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

# 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!

# 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.

# 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

# 05AB1E, 5 bytes

Ý<Ω¬F


Try it online!

# Java (JDK), 22 bytes

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


Try it online!

# Python3, 66 bytes

0/0 if __import__("random").randint(0,int(input()))==1 else exit()

• 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. May 5, 2020 at 9:19
• Ah ok, thanks for the tip! May 27, 2020 at 10:48

# T-SQL, 24 bytes

DECLARE @ INT=10

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


Try it online

# Wj, 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.

• If g uses a range from 1 to input, I don't see how can this crash. May 5, 2020 at 9:58
• @mypronounismonicareinstate The range is probably fixed now.
– user92069
May 6, 2020 at 0:34

# 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.