1
\$\begingroup\$

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. ;)
\$\endgroup\$
4
  • 1
    \$\begingroup\$ 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? \$\endgroup\$
    – Geobits
    Mar 7, 2014 at 2:36
  • \$\begingroup\$ Anything interesting, yes. Answers printing random numbers are not requested. ;) \$\endgroup\$
    – TwoThe
    Mar 7, 2014 at 2:37
  • \$\begingroup\$ 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. \$\endgroup\$
    – intx13
    Mar 7, 2014 at 12:11
  • \$\begingroup\$ 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. \$\endgroup\$
    – TwoThe
    Mar 7, 2014 at 14:43

1 Answer 1

2
\$\begingroup\$

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.

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

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