# Russian Roulette

Write a program which plays Russian Roulette!

If the program is started,

• there should be a 5 in 6 chance of it ending normally after printing "I survived!"
• there should be a 1 in 6 chance of the program crashing. (segmentation fault, etc.)

No input, and no other outputs are allowed.

The randomness must be fair: it must have a uniform probability distribution. This means an uninitialized variable (or a RNG without seed) MOD 6 will not be sufficient.

If the solution works with only one dedicated operating system / platform, you will receive a 6 byte penalty to the score.

Shortest code wins, not sooner than 10 days after first valid answer.

• Can we rely on the underlying runtime to be fair, even if not explicitly guaranteed in documentation? E.g. Python's randrange(5) might be implemented as randrange(MAX_INT)%6. Commented Nov 26, 2012 at 5:35
• To inspire creativity, you might consider granting a bonus to those solutions which don't rely on division by zero. Commented Nov 27, 2012 at 17:04
• Perhaps said bonus should involve dividing the score by 2. Commented Feb 15, 2013 at 15:46
• @JoeZeng : that would have been too much. Usually you can make it a different error, like null pointer reference, etc. for the cost of just a few characters.
– vsz
Commented Feb 15, 2013 at 16:53
• I see. I'm not too experienced in creating scoring conditions for code golf puzzles, so I'm still learning stuff like that. Commented Feb 15, 2013 at 16:55

## VBA - 39/46

I don't love Sean Cheshire's numeric output (though still a good answer, it technically fails the No input, and no other outputs are allowed. from the spec...), plus he uses /0, so here are my alternatives:

?Mid("I Survived!",IIf(Int(6*Rnd),1,0))


This resolves to a Run-time error '5': Invalid procedure when trying to reach character 0 (VBA is 1-based indexing).

n="I Survived!":If Int(6*Rnd) Then ?n Else ?-n


This resolves to a Run-time error '13': Type mismatch when applying a negative switch to a string.

GNU Bash -- 37 bytes

! followed by nothing doesn't trigger history substitution. $[...] is alternative syntax for arithmetic. (...) is a subshell. This solution is portable. bash itself does fork but doesn't exec any other programs. (exit$[$RANDOM%6])&&echo I survived!  # TI-BASIC (TI-84+/SE), 36 bytes startTmr→rand:1/(1<randInt(1,6:"I survived!  There is no input, as the challenge specifies. Output is I survived! if successful, a DIVIDE BY 0 error otherwise. The DIVIDE BY 0 error screen looks like the following: ERR:DIVIDE BY 0 1:Quit 2:Goto  Selecting either option (and returning to home screen if 2 is selected) shows Error after the program call. Examples: prgmCDGFE Error prgmCDGFE I survived! prgmCDGFE I survived! prgmCDGFE Error  Explanation: startTmr→rand:1/(1<randInt(1,6:"I survived! ;full program startTmr→rand ;store the current time into "rand" ; this is necessary because "rand" is 0 after ; factory reset, the default state for TI-BASIC ; submissions randInt(1,6 ;get a random integer in [1,6] 1< ;is greater than 1? 1 if true, 0 if false 1/( ;divide 1 by the result ; throws "DIVIDE BY 0" error if result was ; false "I survived! ;leave this string in "Ans" ;implicitly print "Ans"  Notes: • TI-BASIC is a tokenized language. Byte count does not equal character count. • Lowercase letters are two bytes each. • Lowercase letters can be enabled using this assembly program. • startTmr is a command only on the TI-84+ and TI-84+ SE calculators. Said calculators have different operating systems. • -4 bytes: to produce the error: identity(randInt(0,5 (a zero-size identity matrix will error) Commented Feb 7, 2022 at 12:42 # Japt v1.4.5, 16 bytes 6ö ªí I s¨viv!  Try it -1 byte thanks to @Shaggy! Throws TypeError: U.í is not a function when a random number in the range [0,6) is 0. • 17 bytes Commented Apr 11, 2019 at 7:13 • Actually, it might be an idea to use v1.4.5 for this, just in case ETH adds an N.í() method to v1.4.6. Commented Apr 11, 2019 at 9:11 • Updated - Japt tries hard not to crash on weird programs. I was trying to figure out how to reference a variable that didn't exist (A-Z is defined), but didn't consider calling a method that didn't exist. – dana Commented Apr 11, 2019 at 9:22 • Yeah, N.í() is my "go to" for throwing an error (it used to be N.y()). There are a few other ways of getting an error but they're rarely useful. Commented Apr 11, 2019 at 9:49 • Now, why didn't I think of using a 3rd line?! :\ Commented Apr 11, 2019 at 20:52 # VB.Net, 38 bytes If(CInt(Rnd() * 6), "I survived!", Dir())  Throws : System.ArgumentException 'Dir' function must first be called with a 'PathName' argument. Spaces removed : If(CInt(Rnd()*6),"I survived!",Dir())  • Welcome to PPCG :) Couple of things: you should include your solution's byte count in the header, I'm pretty sure you can remove all the spaces, and, could you use a single character, undefined function name instead of Dir? Also, does VB.Net have bitwise operators? Commented Apr 11, 2019 at 20:35 • Thanks for the welcome and tips - first attempt at code golf! VB.Net will not compile an undefined function name, so I picked an inbuilt with the smallest number of chars. Commented Apr 12, 2019 at 10:00 # C# (Visual C# Interactive Compiler), 48 bytes Write(1/new Random().Next(6)<9?"I survived!":"")  Try it online! Throws Attempted to divide by zero. if a random number in the range [0,6) is 0. # Python 2, 49 bytes import os print["I survived!"][os.urandom(1)<'+']  Try it online! Utilises the idea from this answer of using os.urandom, but we save some bytes by avoiding the ord call. os.urandom(n) generates n random bytes. By checking if they are smaller than chr(math.ceil(256.0/6)) (== '+'), we get an approximately 1/6 chance of True (== 1). • Is the chance one in six though? It's not clear so maybe an explanation will help. Commented May 10, 2019 at 11:14 • @ShaunBebbers explanation added. Since 256%6 != 0 the chance is not exactly one in six, but many answers here approximate those odds slightly. – ArBo Commented May 10, 2019 at 11:21 # Commodore C64/128, C64Mini and compatibles, 10195 92 tokenised BASIC bytes + 6 penalty bytes for only working in Commodore BASIC As Commodore keyword abbreviations, you may enter it as follows:  0a$="i survived":fOi=0to5:a$(i)=a$:nE:a$(rN(.)*6)="":x=6*rN(.) 1?len(a$(x))/len(a$(x))"{left}{left}"a$


I found a six-tokenised byte saving by not using the INT keyword when assigning a value with RND and CBM BASIC will automatically convert the key value to it's integer by ignoring the decimal places.

Non-obfuscated for explanatory purposes:

 0 let a$="i survived" 1 for i=0 to 5 2 let a$(i)=a$3 next i 4 let a$(int(rnd(0)*6)=""
5 let x=int(rnd(0)*6)
6 print len(a$(x))/len(a$(x));"{left}{left}";a$(x)  Firstly, we set up a$ as a string and we later use it as an array; in Microsoft [Commodore] BASIC that appeared on the PET, VIC-20, C64 etc... you can set up the first 11 elements of a zero-indexed array without expressly declaring it first, though it saves space on the variable stack if you do and you have fewer than 11 elements.

So our string array of six elements are set to the string literal of i survived. One of those elements is randomly reset to an empty string.

The x variable is then used to select which of the six will be displayed (one of which is empty). Finally, we try to divide the length of that element with itself; usually this would produce an output of 1 except where the string length of the element is 0, so 0/0 will produce an error (one in six chance) and halt the execution of the symbolic listing; otherwise, the print command moves the cursor position back two spaces to overwrite the output with the string literal of the array element x, or i survived.

This answer assumes that the division by zero error reported by BASIC (which halts the programs execution by the interpreter) is acceptable as the program crashing as cited above.

# VyxalO, 13 bytes

6ʁ℅ḭI ¦⟇d!,


Try it Online!

Fixed thanks to Razetime!

When run offline, 11 bytes (thanks to Aaron Miller)

6ʁ℅ḭI ¦⟇d!


Try it Online!

• The challenge states "no other outputs allowed"; when it errors, it prints 0 to STDOUT Commented May 14, 2021 at 15:49
• You can disable implicit output with the O flag, also I got it to error: Try it Online! Commented May 14, 2021 at 16:17
• @Makonede Fixed. Commented May 14, 2021 at 20:22
• This is a valid 11 byte solution, because when run offline, it errors 1/6 of the time without printing anything. Commented Jul 9, 2021 at 15:01

# Tcl, 64 bytes

puts [expr srand([clock se])*0+int(rand()*6)%6?"I survived":1/0]


Try it online!

## C++, 105 bytes

#include<iostream>
#include<ctime>
int f(){srand(time(0));delete(int*)!(rand()%6);printf("I survived!");}


In C++, delete behavior is that on 0 ( or nullptr ), nothing occurs

Not as cool ( nor dangerous ) as CommitStrip's russian roulette

# Forth (gforth), 51 bytes

include random.fs
: f 1 6 random / ." I Survived!";


Try it online!

### Explanation

Randomly gets a number between 0 and 5 (inclusive) Divides 1 by the result Crashes with divide by zero error if random number is 0

### Code Explanation

include random.fs       \ include the library file necessary for random to work
: f                     \ start a new word definition
1                     \ place 1 on the stack
6 random              \ get a random number between 0 and 5 (inclusive)
/                     \ divide 1 by the result of the random call
." I Survived!"       \ print "I Survived!"
;                       \ end word definition


# JVM Bytecode -noverify, 372 bytes

Works by passing a wrong type to the print function if Random.nextInt(6) is 0.

Hexdump of the class:

$xxd.exe Code.class 00000000: cafe babe 0003 002d 0020 0c00 1900 1f01 .......-. ...... 00000010: 0016 285b 4c6a 6176 612f 6c61 6e67 2f53 ..([Ljava/lang/S 00000020: 7472 696e 673b 2956 0100 106a 6176 612f tring;)V...java/ 00000030: 7574 696c 2f52 616e 646f 6d01 0006 3c69 util/Random...<i 00000040: 6e69 743e 0c00 0400 0803 0000 0006 0700 nit>............ 00000050: 1001 0003 2829 5607 0014 0100 0443 6f64 ....()V......Cod 00000060: 6501 0004 6d61 696e 0900 0900 0101 000a e...main........ 00000070: 536f 7572 6365 4669 6c65 0100 076e 6578 SourceFile...nex 00000080: 7449 6e74 0c00 1100 1a01 0013 6a61 7661 tInt........java 00000090: 2f69 6f2f 5072 696e 7453 7472 6561 6d01 /io/PrintStream. 000000a0: 0007 7072 696e 746c 6e0a 0013 001d 0700 ..println....... 000000b0: 0301 0010 6a61 7661 2f6c 616e 672f 5379 ....java/lang/Sy 000000c0: 7374 656d 0a00 1300 050a 0007 000f 0100 stem............ 000000d0: 0428 4929 4907 000a 0100 036f 7574 0100 .(I)I......out.. 000000e0: 1528 4c6a 6176 612f 6c61 6e67 2f53 7472 .(Ljava/lang/Str 000000f0: 696e 673b 2956 0100 0b49 2073 7572 7669 ing;)V...I survi 00000100: 7665 6421 0800 1b0c 000e 0017 0100 0001 ved!............ 00000110: 0015 4c6a 6176 612f 696f 2f50 7269 6e74 ..Ljava/io/Print 00000120: 5374 7265 616d 3b00 2000 1800 1300 0000 Stream;. ....... 00000130: 0000 0100 0900 0b00 0200 0100 0a00 0000 ................ 00000140: 2900 0500 0100 0000 1db2 000c bb00 1359 )..............Y 00000150: b700 1512 06b6 0012 9a00 0812 06a7 0005 ................ 00000160: 121c b600 16b1 0000 0000 0001 000d 0000 ................ 00000170: 0002 001e ....  Jasmin code to generate: .source "" .class Code .super java/util/Random .method public static main([Ljava/lang/String;)V .limit stack 3 getstatic java/lang/System/out Ljava/io/PrintStream; new java/util/Random dup invokenonvirtual java/util/Random/<init>()V ldc 6 invokevirtual java/util/Random/nextInt(I)I ifne Survive ldc 6 goto Print Survive: ldc "I survived!" Print: invokevirtual java/io/PrintStream/println(Ljava/lang/String;)V return .end method  Must to be run with java -noverify to prevent Java from realizing that the class is messed up. Also it's very wrong this time but CFR output for fun: /* * Decompiled with CFR 0.142. */ import java.io.PrintStream; import java.util.Random; class Code extends Random { public static void main(String[] arrstring) { if (new Random().nextInt(6) != 0) { String string = "I survived!"; } System.out.println((String)6); } } $$$$  • Lol, submit the class file as a cfr 'bug', then I can fix it. Commented Feb 11, 2020 at 17:02 • @lab27 Not exactly something one could fix, it uses a cast that isn't actually valid and some other silliness Commented Feb 11, 2020 at 18:45 # Perl 6, 34 bytes (^6 .roll&&say "I survived!")||die  Throws an exception to crash. Try it online! # MathGolf, 15 bytes 6rw/;'I ûÄ%$╦'!


Try it online!

## Explanation

6r                push [0, 1, 2, 3, 4, 5]
w               random item from list
/              divide implicit zero by the random element
;             discard TOS (needed for non-crashes)
'I           push single character "I"
space character
ûÄ%       push "Ä%"
\$      convert to ordinal (from base 256 to base 10)
╦     fetch single dictionary word "survived"
'!   push single character "!"


With some better handling of dictionary fetches, this could probably be 2-3 bytes shorter, but that's not getting implemented within the immediate future.

# Octave 33 bytes

{"I_Survived!"}(1/(rand(1)>0.16))


Approx each 6 time, there is a warning: division by zero error: index (inf): subscripts must be either integers 1 to (2^31)-1 or logicals

• It prints way more than "I survived" and the rules say no other input than that is allowed. Commented May 14, 2021 at 7:52

# Java, 98 bytes

interface m{static void main(String[]a){System.out.print(Math.random()<.16?1/0:"I survived!");}}


Try it online

Bonus answer that allocates 2^60 bytes of memory instead of dividing by 0:

import sun.misc.Unsafe;import java.lang.reflect.*;interface M{static void main(String[]a)throws Exception{Field f=Unsafe.class.getDeclaredField("theUnsafe");f.setAccessible(1>0);((Unsafe)f.get(null)).allocateMemory(Math.random()<.16?1L<<60:0);System.out.print("I survived!");}}


TIO

# Runic Enchantments, 23 bytes

>6'RA0)?;"I survived!"@


Try it online!

Nothing too special here. Just an IP that picks a random number [0-6) and prints the required string if the random value isn't 0.

Required string contains two vs which also creates an IP each that get terminated after 100k execution steps (otherwise TIO would produce instant results and the > wouldn't be needed).

# C (gcc), 58 53 bytes

-5 bytes by using &i in place of time(0) - thanks ceilingcat

Floating point exception when rand() is divisible by 6

main(i){i/=(srand(&i)+rand()%6);puts("I survived!");}


Try it online!

• ceilingcat that was in my original code but I wasn't sure if the pointer value changed across runs (I'm not that good at C) Commented Jan 28, 2020 at 0:29
• 44 bytes Commented Jan 28, 2020 at 2:35

# Microscript II, 20 bytes

6R("I survived!"ph)f


Generates a random integer on [0,5], printing the message and halting if the generated integer is nonzero. If the generated integer is 0, it instead crashes with an IllegalArgumentException because the f instruction is invoked on a nonstring value

• Can you add an explanation for those not familiar with Microscript?
– mkst
Commented Jun 17, 2020 at 10:03
• @streetster done Commented Jun 24, 2020 at 3:17

# MATLAB/Octave, 37 bytes

a=1:5;a(randi(6));disp("I survived!")


Try it online!
It has 1 in 6 chances to crash with indexing error, when randi returns 6.

# PPL, 96 bytes

importRandom
declarex=Random.random()*6
ifx>1{
printLine("I survived!")
}
ifx<=1{
printLine(f)
}


Very similar code to the Minceraft problem. Runs on PPL v1.0.10.

Soon, I will publish an official PPL website where you can create and run PPL code. I am still working on it.

## Explanation

The first line imports Random.

Then, we set x to Random.random() (random float between 0 and 1) multiplied by 6.

Since neither else nor ternary exists in PPL, we must use two diverging if-statements. One prints the string I survived! to STDOUT (if x is greater than 1) while the other errors because f is undefined.

# 05AB1E, 12 bytes

Ties hyper-neutrino's Jelly answer for #1.

“IÐ–d!“6LΩiF


Try it online!

“...“6LΩiF  # trimmed program
“...“       # push "I survived!"
i   # if...
Ω    # random element in...
L     # [1, 2, 3, ...,
6      # ..., literal...
L     # ]...
i   # is 1...
F  # then do nothing...
“...“       # "I survived!"...
F  # times
# implicit output


# Ly, 19 bytes

6?/<"I survived!"&o


Try it online!

This is similar to some of the other entries. It generates a random number between 0 and 5, then crashes with a division by zero error if 0 was the result.

6?                   - generate a random number in 0-5
/                  - divide, fails with division by zero 1/6 of the time
<                 - switch to a clean stack
"I survived!"    - push the output string (codepoints)
&o  - print the stack a characters


# Julia 1.0, 32 bytes

0%rand(0:5);print("I survived!")


Try it online!

# APOL, 28 bytes

Dead simple, just uses the "divide by 0" method.

/(1 ∿(0 5));p("I survived!")

# <>^v, 18 bytes

5#=V"I survived!";


run online

### Explanation

5#=V"I survived!";
5                  Push 5 to stack
#                 Push random number in range [0, <top of stack>]
=                Skip next instruction if the top two elements (5 and the random number)
of the stack are equal
V               Try reading the value of register 'v'. This should crash because it was
never assigned a value, its setter 'v' being a keyword
"              String start
I survived!   Contents of the string
"  String end
; Print with newline


# Python 3, 54 bytes

import random;1/random.randrange(5);print'I survived!'


On 1/0 it will crash:

Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero


# Rattle, 21 19 bytes

=?6/p"I survived!"


Try it Online!

There's a 5/6 chance that this just prints I survived, and a 1/6 chance that this exits with an error:

Error: float division by zero at command index 1 [/]


# Explanation

=?6           sets the top of the stack to a random value from 0-5 inclusive
/         divides the top of the stack by the top of the stack.
This works fine except for when the top of the stack is 0
b"..."   adds "I survived!" to the output buffer, implicit output (p" " would work here too)