# Make your code error, but only sometimes!

Your task is to create a program or function which randomly errors. Specifically, there must be a nonzero probability of erroring, but also a nonzero probability of running without error.

An error is anything that causes a program to terminate abnormally, such as dividing by zero or using an uninitialized variable. This also includes runtime errors, syntax errors and errors while compiling. Statements which manually throw an error, such as JavaScript's throw are allowed.

This program doesn't need to do anything if it doesn't error, other than exiting gracefully.

This is , so shortest answer per language wins.

Note: For this challenge, "randomly" follows the current consensus (so no using undefined behavior or uninitialized memory for randomness), and the program must be capable of producing both outputs if run or compiled multiple times (so a random number with the same seed is not valid)

• I think it's a boring challenge. A lot of the answers are doing basically the same thing and there's little room for golfing. Note that downvotes don't necessarily indicate a challenge is ill-specified -- there's close votes for that. – xnor Apr 10 '20 at 15:43
• Separately, I'm not so sure it's clear, since two of the upvoted answers rely on the state of memory when the program starts, despite the question in my earlier comment. – xnor Apr 10 '20 at 15:45
• I don't really think the current definition of random, although settled, is really clear. For example, the python answer and the brainfuck answer below uses some degree of undefined behavior. The c answer, although fits that definition, won't really crash because of the lack of srand. So, I don't really like challenges based on such loose definitions (and when such definition matters a lot). I would change it to upvote if the question is updated, say, to, the program may or may not crash after a rerun/recompile. – newbie Apr 10 '20 at 15:47
• It seems like so many users are posting 1/(random integer) here... – Scratch---Cat Apr 11 '20 at 1:33
• @xnor: agreed, this turns out to be somewhat interesting in some assembly languages, but most high level languages make unpredictable behaviour intentionally difficult other than integer division, or a few other things where 0 is special. – Peter Cordes Apr 11 '20 at 4:40

# Wolfram Language (Mathematica), 17 bytes

1/RandomInteger[]


Try it online!

# brainfuck, 2 bytes

Sometimes segfaults, sometimes doesn't.

<.


Try it online!

• Isn't this implementation based, not random? – Redwolf Programs Apr 10 '20 at 14:27
• sometimes malloc allocates a bigger page so you can read something before the tape, and sometimes malloc allocates smaller page so reading something before the tape segfaults – Kamila Szewczyk Apr 10 '20 at 14:28
• personally after mashing the run button for a while, I've seen two segfaults so far – Kamila Szewczyk Apr 10 '20 at 14:29
• That's just because you're mashing the run button too fast. Also, this uses neither options 1, 2, nor 3 from the meta consensus. – S.S. Anne Apr 10 '20 at 23:34
• +[<[-]+] doesn't segfault on TIO, so I don't think this is valid. In fact, if the [-] is removed, it halts, so the tape is seemingly looped. – the default. Apr 11 '20 at 3:31

# Erlang (escript), 22 bytes

Just like the other answers...

f()->1/rand:uniform().


Try it online!

# SQL (Oracle), 3733 Bytes

SELECT 1/INSTR(UID,3) FROM DUAL;


Fails if the User Session doesn't have a 3 in it. Edit: Was using SYSDATE, but UID is shorter. Although I'm kinda sad. I liked having a function that worked in March, but not February.

• Although this answer is interesting, the requirements state it must be random. I'm not very good with SQL, but wouldn't SELECT 1/RAND() work (errors if RAND() is 0, which has a very, very small chance of happening)? – Redwolf Programs Apr 10 '20 at 19:38
• @RedwolfPrograms RAND isn't a valid function in Oracle. – Del Apr 10 '20 at 19:39
• I have not used oracle for ages. But would the work instead ? x int:=1/INSTR(UID,3) – t-clausen.dk Apr 11 '20 at 13:10

# Keg, 3 bytes

~~/


Try it online!

This works by pushing two random numbers onto the stack ane dividing them. If the second number is 0, then it errors out.

There is indeed a chance for the second number to be 0, but it is highly unlikely that it will be, as the random range used is quite large. But highly unlikely != zero.

If I were to estimate the odds of erroring out, I'd say it'd be something like 1 in ((int​(​"​9​"​*​3234​) ** 2) + 21) * 2.

• Hey, look, it worked: TypeError: integer argument expected, got float – S.S. Anne Apr 10 '20 at 23:48
• With a higher chance of erroring... ~:/ – user92069 Apr 11 '20 at 2:58
• @petStorm that's still the same chance. – lyxal Apr 11 '20 at 3:03
• Is "highly unlikely != non-zero" intended to be "highly unlikely != zero"? – L. F. Apr 11 '20 at 3:39

# Bash, 1/32767 chance of success, 12 bytes

exit \$RANDOM


# Red - 24 bytes

x: now/time 1 / x/minute

• Welcome to the site, I've tried to correct the code formatting in your answer. You can revise it if I have something wrong. Let me know if you are having trouble with the formatting. – Wheat Wizard Apr 15 '20 at 13:13

## Windows Batch, 1715 bytes

set/a1/%random%
• I don't think you need the x=. – Neil Apr 10 '20 at 23:34
• indeed, -2 bytes thanks to @Neil – peter ferrie Apr 17 '20 at 22:42

## VBA, 46423525 13 bytes

Thanks to @Taylor Scott

?1/Int(9*Rnd)


This code only works inside the Immediate Window.

• If you make this an immediate window function, you can get it down to ?1/Int(9*rnd) – Taylor Scott May 26 '20 at 19:48
• @Taylor Scott, I didn't know you can do that. Thanks, mate. – noise Jul 1 '20 at 1:53

# Befunge-98 (FBBI), 4 bytes

?@
=


Executing = is of the only ways I can get Befunge to actually error (a / won't work there because 0/0 evaulates to 0 in Befunge-98 and asks the user in Befunge-93).

Try it online!

The ? turns the IP in a random direction. If it goes horizontally (1/2 chance), it hits the @ and terminates successfully. If it goes vertically, it hits the = and uses the empty stack (interpreted as the empty string) as the argument. This throws a segfault in FBBI (which implements = as a C system() call).

# 05AB1E, 3 bytes

®Ωι


Try it online! Has a $$\\frac12\$$ chance of erroring.

®Ωι  # full program
ι  # uninterleave...
Ω   # random element from...
®    # register_c (starts at -1)...
# (implicit) split into separate characters...
ι  # lists which were interleaved together into...
# (implicit) stack is empty, so bottom of stack...
# (implicit) converted to list


-1 split into separate characters is ["-", "1"], so it picks a random element from there. Either it picks 1 and uninterleaves 1 list which was interleaved into ["1"] (one list interleaved is the same list, so it returns ["1"]), or it picks -, and tries to uninterleave - lists which were interleaved together into ["-"]. Unlike ï, the built-in integer conversion function, it doesn't handle errors (if it can't be converted to an integer). So, it throws an error.

# Ruby, 10 bytes

1.div rand


Can't do 1/rand, since 1/0.0 in Ruby is Infinity, not an error.

Try it online!