[Question]
  [
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.
  
[Answer]
## PHP 38 bytes
```
<?~$$s[rand(+$s=sssss,5)]?>I survived!
```
Placing a `+` before a non-numeric string will evaluate to `0`. Should `rand(0,5)` return `5`, `$s[rand(0,5)]` will be the empty string (since `$s` is only five characters long), and subsequently `$$s[rand(0,5)]` will be an uninitialized variable. Attempting to take the inversion will halt on Unsupported Operand Type. Any other value, `0-4` will return `s`, and because `$s` is defined, you will survive.
Note: as of php version 4.2.0, the random number generator is [seeded automatically](http://php.net/ChangeLog-4.php#4.2.0).
[Answer]
## R 30
```
"I survived!"[6*runif(1)<5||Z]
```
One time out of six, it will throw an error: `Error: object 'Z' not found`
[Answer]
## Dyalog APL - 25 22 21 20 Charachters
```
'I Survived!'⊣1÷6⊤?6
```
Prints `DOMAIN ERROR` as the error, due to division by zero.
Shortest non-division by zero solution I could come up with is 23 characters.
```
('I Survived!'1)[~6⍷?6]
```
It throws an `INDEX ERROR`
[Try it here](http://www.tryapl.org/)
[APL Font here](http://wiki.nars2000.org/index.php/APL_Font)
[Answer]
## Python, 96
```
from ctypes import*
from random import*
randrange(5)or pointer(c_int())[9**9]
print'I survived!'
```
If `randrange(5)` returns 0, then python will crash due to a segmentation fault.
[Answer]
## Ruby, 24-28
```
p rand(6)<5?"I survived!":1/0
```
Approx each 6 time, there is a `ZeroDivisionError`
There is even a shorter version with 24 characters (Thanks to ugoren and histocrat):
```
6/rand(6);p"I survived!"
```
If you don't accept the `"` in the output, then I need 3 more characters. The first option (`puts`) adds a newline, the second (`$><<`) makes no newline:
```
6/rand(6);puts"I survived!"
6/rand(6);$><<"I survived!"
```
There is a [question about random number in ruby at SO](https://stackoverflow.com/questions/198460/how-to-get-a-random-number-in-ruby).
The seed with `srand` is automatically called with the seed being from the current time if it wasn't already called. (see [Julians comment](https://stackoverflow.com/questions/198460/how-to-get-a-random-number-in-ruby#comment2164872_198486))
---
Primo had the idea for an extra bonus [to those solutions which don't rely on division by zero](https://codegolf.stackexchange.com/questions/9062/russian-roulette/9083#comment17890_9062).
My first solution can be shortened (28 characters) with a `undefined local variable or method ``a' for main:Object (NameError)`
```
p rand(6)<5?"I survived!":a
```
[Answer]
## J, 18
```
'I survived!'[q:?6
```
Failing with `domain error` when trying to factorise 0.
[Answer]
## vba, 27
```
?1/int(6*rnd),"I Survived!"
```
used in immediate window.  
On failure, an error window stating:  
![division by zero](https://i.stack.imgur.com/nnNrR.jpg)  
appears
[Answer]
# Befunge - 48 chars
```
 v >91+"!devi"v
/?>?<v"I surv"<
 / / :
   :,_@#
```
Befunge's only randomness is the `?` operator, which sends you heading in one of four posible directions (`1/4` chance). By blocking one or two directions, you have `1/3` or `1/2` chance, and by combining these, you get `1/6` chance to get out of the program "alive".
The program crashes by doing a divive-by-zero. I guess it's implementation-specific what will happen (on Wikipedia it says the program should ask for the desired answer), but [befungee.py](https://github.com/programble/befungee) sort of crashes, or exits angrily:
```
$ for i in {1..6} ; do ./befungee.py roulette.befunge ; done
Error (1,2): integer division or modulo by zero
Error (3,2): integer division or modulo by zero
Error (1,2): integer division or modulo by zero
I survived!
Error (0,1): integer division or modulo by zero
I survived!
```
[Answer]
# [05AB1E](https://github.com/Adriandmen/05AB1E/wiki/Commands), ~~13~~ 12 [bytes](https://github.com/Adriandmen/05AB1E/wiki/Codepage)
```
6LΩiFë“IÐ–d!
```
-1 byte thanks to *@Emigna*.
05AB1E actually shouldn't be able to error at all, but since the new version of 05AB1E still has some issues compared to the legacy version, I can take that to my advantage to error out for this challenge.
[Try it online.](https://tio.run/##yy9OTMpM/f/fzOfcyky3w6sfNczxPDzhUcPkFMX//wE)
**Explanation:**
```
6L          # Create the list [1,2,3,4,5,6]
  Ω         # Get a random choice from this list
   i        # If it is 1:
    F       #  Do a ranged loop, which currently results in a "(RuntimeError) Could not
            #  convert  to integer." error when no argument is given
   ë        # Else:
    “IÐ–d!  #  Push dictionary string "I survived!" (which is output implicitly as result)
```
[See this 05AB1E tip of mine (section *How to use the dictionary?*)](https://codegolf.stackexchange.com/a/166851/52210) to understand why `“IÐ–d!` is `"I survived!"`.
[Answer]
## C, 67 65 62 chars
`rand()%8` doesn't lose fairness. Division crashes for `t=0`, gives true for 1 and 2 (retry), gives false for 3..7 (survived).  
**EDIT:** The previous version used a temporary variable, which ended up completely unneeded. `2/(rand()%8)` implements both needed conditions.
```
main(){
        for(srand(time(0));2/(rand()%8););
        puts("I survived!");
}
```
[Answer]
# T-SQL 56 44 40 + 6
```
 if 1/cast(ceiling(rand()*6)-1as int)<2print'I Survived!'
```
Credit Sean Cheshire for calling out cast as unnecessary
```
 if 1/ceiling(rand()*6-1)<2print'I Survived!'
```
Credit personal message from Sean Cheshire for suggestion to change ceiling to floor.
```
 if 1/floor(rand()*6)<1print'I Survived!'
```
Death Err Msg:
Msg 8134, Level 16, State 1, Line 3
Divide by zero error encountered.
[Answer]
Using the usual divide by zero method:
Perl 5.8 Version
```
1/(int rand 6)&&print "I survived!"
```
Perl 5.10 Version
```
1/(int rand 6)&&say "I survived!"
```
On failure, these will display:
```
Illegal division by zero at -e line 1.
```
Using the bless function which is used for creating objects in perl.
Perl 5.8 Version
```
print (int rand 6?"I survived!":bless me);
```
Perl 5.10 Version
```
say (int rand 6?"I survived!":bless me);
```
On failure, these will display:
```
Can't bless non-reference value at -e line 1.
```
[Answer]
## GolfScript, 21 chars
```
,6rand/;'I survived!'
```
Like most of the answers, this one has a one in six chance of crashing with a ZeroDivisionError. The shortest solution I could manage *without* using division by zero is 23 chars:
```
5,6rand=+;'I survived!'
```
which has a 1/6 chance of crashing with `undefined method `+' for nil:NilClass (NoMethodError)`.
(Ps. While developing this, I found what might be a bug in the GolfScript interpreter: code like `0,1>` appears to leave a `nil` value on the stack, which will later crash the program if you try to do anything with that value except pop it off and throw it away with `;`. Unfortunately, the fact that I do need to use the value somehow to trigger a crash means that even exploiting this bug didn't help me get below 23 chars.)
[Answer]
## **Python, 70 characters**
With inspiration from grc's answer.
```
from random import*
if randrange(5)<1:exec'()'*9**5
print'I survived!'
```
randrange(5) returns a value between 0 and 5.  
If it returns a 0, Python crashes while attempting to exec(ute) a string of code that contains 9^5 sets of parentheses.
[Answer]
# PHP - 30 bytes
```
<?rand(0,5)?:~[]?>I survived!
```
Requires PHP 5.4+ for the short array syntax, invalid operator idea shamelessly stolen from @primo.
As stated, [`rand()` is automatically seeded on first use](http://lxr.php.net/xref/PHP_5_4/ext/standard/rand.c#63).
[Answer]
# Befunge, 38
```
v>25*"!devivrus I",,,,,,,,,,,@
?^
v
?^
<1
```
Pretty straight-forward. Crashing is done by pushing 1s onto the stack until it overflows. I made a few attempts at cutting out those 11 commas and replacing them with some more efficient loop to print everything, but couldn't get it under 11 characters.
Note that counting characters in Befunge is a little tricky... For instance there's only one character on the third line, but I'm counting an extra one there since execution could travel through that location.
[Answer]
# Python3.8.10 : 45
```
int(set('12345 ').pop());print('I survived!')
```
I hope it follows the challenge specification.
I did some statistics with this method and it seems to have a uniform probability distribution
(the program crashes 10416 times over 60000 calls from the console)
[Answer]
**Javascript, 42**
`(Math.random()*6|0)?alert('i survived!'):b`
The bitwise or floors the result of the multiplication thus a value between 0 and 5 results. 0 gets implictly casted to false, so in 5 of 6 cases the alert appears in the 6th case a certain `b` is referenced, crashing the process.
[Answer]
## CMD Shell (Win XP or later), 40 +6
I'm only doing this one because DOS is not something that should even be thought of for code golf, and the whitespace is important
```
set/a1/(%RANDOM% %% 6)&&echo I Survived!
```
On failure, it will print 
> 
> Divide by zero error.
> 
> 
> 
[Answer]
# R, 50 44 42 36
```
ifelse(!is.na(sample(c(NA,1:5),1)),'I Survived!',)
```
```
ifelse(floor(runif(1,0,5))>0,'I Survived!',)
```
```
ifelse(floor(runif(1,0,5)),'I Survived!',)
```
```
ifelse(sample(0:5,1),'I Survived!',)
```
Death Err Message:
> 
> Error in ifelse(!is.na(1/sample(c(NA, 1:5), 1)), "I Survived!", ) : 
>  argument "no" is missing, with no default
> 
> 
> 
[Answer]
## Emacs-Lisp, 42 characters
```
(if (= (random 6) 5) 
    z (message "I survived!")
    )
```
[Answer]
## Javascript, 40 chars
In Javascript the divide-by-zero trick doesn't even work: it just returns Infinity. Therefore, referencing a non-existing variable:
```
alert(6*Math.random()|0?"I survived!":f)
```
Not so short, though fun :)
[Answer]
**PowerShell, 40 Chars**
```
IF(6/(Get-Random -Max 6)){'I Survived!'}
```
On Failure: "Attempted to divide by zero."
[Answer]
# Python, 53 bytes
Here's a short 53 byte python index out of range program:
```
import time
[0][time.time()%6<1]
print("I survived!")
```
[Answer]
# [Befunge-98 (FBBI)](https://github.com/catseye/FBBI), 28 bytes
```
r?<?
"< <@,ka"I survived!
 =
```
[Try it online!](https://tio.run/##S0pNK81LT9W1tPj/v8jexp5LyUbBxkEnO1HJU6G4tKgssyw1RZFLwfb/fwA "Befunge-98 (FBBI) – Try It Online")
Errors with a segmentation fault.
## Explanation
Befunge-98 adds some extra features (`r`, `k`, and `=`) that can be used to further golf [Joe K's Befunge-93 answer](https://codegolf.stackexchange.com/a/10670/95679).
The first line (`r?<?`) calculates the 1/6 chance. The `r` initially reflects the IP to the left, where it reaches the second `?` and goes in a random direction. If it goes right, the `r` reflects it and it tries again. If it goes up or down, it moves onto the second line, and if it goes left, it moves onto the first `?`. There are three ways the IP can leave, so there is a 1/3 chance it moves onto the first `?`.
If it gets there, it again goes in a random direction. If it goes horizontally, it gets reflected (with either `r` or `<`) and tries again. This means that there are only two ways that the IP can leave, and they are equally likely. Either it goes down onto the second line, or it goes up and hits the `=` on the third line.
There is a 1/6 chance that `=` is executed (1/3 from the second `?` times 1/2 from the first). If it is, it does a system-execute call on the stack (which is empty and interpreted as an empty string). FBBI's implementation of this runs a C `system()` call on the empty string, which segfaults.
This leaves a 5/6 chance that the IP reaches the second line. If it does, it gets sent left with `<`, and pushes the string `"!devivrus I"` onto the stack. `ak,` prints the top 11 characters of the stack, outputting `I survived!` Finally, `@` ends the program.
[Answer]
# [Jelly](https://github.com/DennisMitchell/jelly), 12 bytes
```
6X’’Xṛ“`:ƭ÷»
```
[Try it online!](https://tio.run/##y0rNyan8/98s4lHDTCCKeLhz9qOGOQlWx9Ye3n5o9///AA "Jelly – Try It Online")
## Explanation
```
6X            Generate a random number between 1 and 6 inclusive (using python randrange)
  ’’          Decrement it twice
    X         Generate a random number from 1 to the above value. 0 if the value is 0. Error if the value is negative (1/6 chance)
     ṛ        Right Argument; ignore the above value and instead return
      “`:ƭ÷»  "I survived!"
```
Unfortunately, taking the reciprocal of 0, dividing or integer-dividing by 0, and modulo by 0 all give either inf or nan, and don't actually error.
[Answer]
**Java, 149**
```
public class R{public static void main(String[]s){int[]a={1,1,1,1,1};System.out.println(a[new java.util.Random().nextInt(7)]>0?"I survived!":"");}}
```
Fails with an "Array out of bounds" error. Managed to shave a few characters by using anonymous Random object (no imports).
[Answer]
## Groovy, 39
```
1/new Random().next(6);print"I survived!"
```
Picks a random number between 0 and 5 inclusive. If 0, throws a divide by zero exception.
[Answer]
## Python (56), Haskell (77)
This crashes with an IndexError when the generated number is 1:
```
from random import*
print['I survived!'][1/randint(1,7)]
```
The Haskell solution has the same idea:
```
import System.Random
main=putStrLn.(["I survived!"]!!).div 1=<<randomRIO(1,6)
```
[Answer]
## Python, 59 55 53, 65 59 56
```
import os
1/(ord(os.urandom(1))%6)
print"I survived!"
```
`ZeroDivisionError` when `ord(os.urandom(1))%6` evaluates to 0
```
import os
print(["I survived!"]*5)[ord(os.urandom(1))%6]
```
`IndexError` when `ord(os.urandom(1))%6` evaluates to 5
]