18
\$\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\$
18
  • 19
    \$\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, 2020 at 15:43
  • 3
    \$\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, 2020 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, 2020 at 15:47
  • 5
    \$\begingroup\$ It seems like so many users are posting 1/(random integer) here... \$\endgroup\$ Apr 11, 2020 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\$ Apr 11, 2020 at 4:40

65 Answers 65

1 2
3
1
\$\begingroup\$

Volatile, 3 bytes

~~/

~ pushes a random number onto the stack (which may include zero), and / pops two numbers and divides them. If the first ~ pushes a zero, then the program errors.

\$\endgroup\$
0
\$\begingroup\$

(plain) pdfTeX, 34 bytes

\ifcase\pdfuniformdeviate9}\fi\bye
\$\endgroup\$
0
\$\begingroup\$

Perl 5, 8 bytes

1/($$%2)

Here $$ is current PID.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ The current PID is not considered random for this challenge. \$\endgroup\$ Aug 22, 2021 at 18:55
0
\$\begingroup\$

Commodore BASIC (Commodore C64/128, VIC-20, C16/+4, PET, Ultimate64, THEC64 etc)

0 on6*rnd(.)goto10,20,30,40,50:goto
10 print"working":goto
20 print"working":goto
30 print"working":goto
40 print"working":goto

I've put a prompt in just to show that it does work sometimes too. Essentially, it breaks when the random number generated is rounded to 5 (it generates floating point numbers, but the BASIC interpreter ignores the decimal places).

Commodore C64 BASIC working or not

\$\endgroup\$
0
\$\begingroup\$

C (gcc), 30 bytes

main(){int*x=0x12345678;*x=1;}

Try it online!

Will segfault most of the time, but in the case that the program memory gets allocated to that address, it will NOT segfault.

\$\endgroup\$
1
  • \$\begingroup\$ This isn't considered random for this challenge, similarly to uninitialized memory or your program's PID \$\endgroup\$ Aug 12 at 17:16
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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