# Predictable unpredictability [closed]

Ever programmer knows, that programs need to be predictable. Any code that is not predictable will do something undefined and therefore most likely crash or worse. But when I tried to figure out what Java's lazySet() method does, I came up with the idea

Is it possible to write a program that is unpredictable, yet works fine and does something useful/interesting?

So that will be your task. Do not avoid chaos, but utilize it. Whether you use the actual lazySet, thread timing, random or uninitialized variables is up to you. The following rules apply:

1. The code-path must be unpredictable. That is: if I would debug it, the program would take a "random" path each time.

2. The program must do something useful/interesting and handle all possible (unpredictable) states somehow. No crashes! (Core dumps are neither useful nor interesting!)

3. You are not allowed to check something in a loop till it has the desired value. You must work with whatever is stored in there and do something with it.

4. You can use any language, except those specifically optimized for this contest.

This is a creative question so the one with the most votes wins. Please note that programs doing common stuff like gambling, printing random numbers or generating RPG loot are too well-known to be considered interesting. Please surprise us.

Bonus points should be given for:

• The creativity of the choice/design of unpredictability (Math.random isn't that creative).
• The creativity in management of unpredictability.
• Everything genius. ;)

## closed as too broad by Geobits, Justin, 23fc9a62-56de-47fb-97b4-737890, Peter Taylor, ProgramFOXMar 7 '14 at 9:08

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

• This is... pretty broad. The objective is to do "anything based on randomness", right? Gambling, Monte Carlo Go AIs, RPG loot tables, pretty drawings, are there any limits here? – Geobits Mar 7 '14 at 2:36
• Anything interesting, yes. Answers printing random numbers are not requested. ;) – TwoThe Mar 7 '14 at 2:37
• How about modifying the challenge to be code whose program flow is random but output is nonetheless deterministic? Every time the program/function is run (with the same arguments) it should return the same output, but a trace should indicate the program followed a different path of instructions. – intx13 Mar 7 '14 at 12:11
• Too many answers for a creative question... Hmm.. where would the creativity find its room, when I narrow it down and put it into a cage? "Write a simple random code" I can do myself, I was specifically asking for creative ideas on what you could do with chaos in coding. – TwoThe Mar 7 '14 at 14:43

# Ruby

This works only on Windows.

if aTerribleErrorOccurredAndWeNeedToKillTheScriptNow
c = ($$% 94 + 33).chr puts "#{c * 50}\n#{c + "KILLING SCRIPT".center(48) + c}\n#{c * 50}" taskkill /PID #{$$} /F
end


The process ID, , is unpredictable. However, this will always kill the currently running script, while printing an error message bordered by an unpredictable character like this:

##################################################
#                 KILLING SCRIPT                 #
##################################################


Of course, exit or quit do the same thing, but this one kills Ruby forcefully with the OS! And it also bypasses at_exit handlers.

• That would violate rule #1: the program always follows the same execution path. – TwoThe Mar 7 '14 at 2:38
• @TwoThe Whoops, fixed! – Doorknob Mar 7 '14 at 2:41
• It still does. Although c will be random, if I would debug it, it would always take the same path. – TwoThe Mar 7 '14 at 2:46
• @TwoThe No, try running it multiple times and it outputs differently each time. – Doorknob Mar 7 '14 at 2:55
• Where does aTerribleErrorOccurredAndWeNeedToKillTheScriptNow come from? – TheDoctor Mar 7 '14 at 3:15