10
\$\begingroup\$

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)

\$\endgroup\$
12
  • 15
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Apr 10 '20 at 15:43
  • 2
    \$\begingroup\$ 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. \$\endgroup\$ – xnor Apr 10 '20 at 15:45
  • 4
    \$\begingroup\$ 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. \$\endgroup\$ – newbie Apr 10 '20 at 15:47
  • 3
    \$\begingroup\$ It seems like so many users are posting 1/(random integer) here... \$\endgroup\$ – Scratch---Cat Apr 11 '20 at 1:33
  • 1
    \$\begingroup\$ @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. \$\endgroup\$ – Peter Cordes Apr 11 '20 at 4:40

42 Answers 42

1
2
1
\$\begingroup\$

Wolfram Language (Mathematica), 17 bytes

1/RandomInteger[]

Try it online!

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

brainfuck, 2 bytes

Sometimes segfaults, sometimes doesn't.

<.

Try it online!

\$\endgroup\$
5
  • 4
    \$\begingroup\$ Isn't this implementation based, not random? \$\endgroup\$ – Redwolf Programs Apr 10 '20 at 14:27
  • \$\begingroup\$ 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 \$\endgroup\$ – Kamila Szewczyk Apr 10 '20 at 14:28
  • \$\begingroup\$ personally after mashing the run button for a while, I've seen two segfaults so far \$\endgroup\$ – Kamila Szewczyk Apr 10 '20 at 14:29
  • \$\begingroup\$ 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. \$\endgroup\$ – S.S. Anne Apr 10 '20 at 23:34
  • \$\begingroup\$ +[<[-]+] 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. \$\endgroup\$ – the default. Apr 11 '20 at 3:31
1
\$\begingroup\$

Erlang (escript), 22 bytes

Just like the other answers...

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

Try it online!

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

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.

\$\endgroup\$
3
  • \$\begingroup\$ 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)? \$\endgroup\$ – Redwolf Programs Apr 10 '20 at 19:38
  • 1
    \$\begingroup\$ @RedwolfPrograms RAND isn't a valid function in Oracle. \$\endgroup\$ – Del Apr 10 '20 at 19:39
  • \$\begingroup\$ I have not used oracle for ages. But would the work instead ? x int:=1/INSTR(UID,3) \$\endgroup\$ – t-clausen.dk Apr 11 '20 at 13:10
1
\$\begingroup\$

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.

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

Bash, 1/32767 chance of success, 12 bytes

exit $RANDOM
\$\endgroup\$
1
\$\begingroup\$

Red - 24 bytes

x: now/time 1 / x/minute
\$\endgroup\$
1
  • \$\begingroup\$ 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. \$\endgroup\$ – Wheat Wizard Apr 15 '20 at 13:13
1
\$\begingroup\$

Windows Batch, 1715 bytes

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

VBA, 46 42 35 25 13 bytes

Thanks to @Taylor Scott

?1/Int(9*Rnd)

This code only works inside the Immediate Window.

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

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

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

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.

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

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!

\$\endgroup\$
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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