104
\$\begingroup\$

You need to produce output that is non-deterministic.

In this case, this will be defined to mean that the output will not always be the same result.

Rules:

  • A pseudo-random number generator that always has the same seed does not count.

  • You can rely on the program being run at a different (unknown) time each execution.

  • Your code's process id (if it's not fixed by the interpreter) can be assumed to be non-deterministic.

  • You may rely on web-based randomness.

  • Your code may not take non-empty input. Related meta post.

  • The program is not required to halt, but the output must be displayed.

Leaderboard

function answersUrl(a){return"https://api.stackexchange.com/2.2/questions/"+QUESTION_ID+"/answers?page="+a+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+ANSWER_FILTER}function commentUrl(a,b){return"https://api.stackexchange.com/2.2/answers/"+b.join(";")+"/comments?page="+a+"&pagesize=100&order=desc&sort=creation&site=codegolf&filter="+COMMENT_FILTER}function getAnswers(){jQuery.ajax({url:answersUrl(answer_page++),method:"get",dataType:"jsonp",crossDomain:!0,success:function(a){answers.push.apply(answers,a.items),answers_hash=[],answer_ids=[],a.items.forEach(function(a){a.comments=[];var b=+a.share_link.match(/\d+/);answer_ids.push(b),answers_hash[b]=a}),a.has_more||(more_answers=!1),comment_page=1,getComments()}})}function getComments(){jQuery.ajax({url:commentUrl(comment_page++,answer_ids),method:"get",dataType:"jsonp",crossDomain:!0,success:function(a){a.items.forEach(function(a){a.owner.user_id===OVERRIDE_USER&&answers_hash[a.post_id].comments.push(a)}),a.has_more?getComments():more_answers?getAnswers():process()}})}function getAuthorName(a){return a.owner.display_name}function process(){var a=[];answers.forEach(function(b){var c=b.body;b.comments.forEach(function(a){OVERRIDE_REG.test(a.body)&&(c="<h1>"+a.body.replace(OVERRIDE_REG,"")+"</h1>")});var d=c.match(SCORE_REG);d?a.push({user:getAuthorName(b),size:+d[2],language:d[1],link:b.share_link}):console.log(c)}),a.sort(function(a,b){var c=a.size,d=b.size;return c-d});var b={},c=1,d=null,e=1;a.forEach(function(a){a.size!=d&&(e=c),d=a.size,++c;var f=jQuery("#answer-template").html();f=f.replace("{{PLACE}}",e+".").replace("{{NAME}}",a.user).replace("{{LANGUAGE}}",a.language).replace("{{SIZE}}",a.size).replace("{{LINK}}",a.link),f=jQuery(f),jQuery("#answers").append(f);var g=a.language;g=jQuery("<a>"+g+"</a>").text(),b[g]=b[g]||{lang:a.language,lang_raw:g,user:a.user,size:a.size,link:a.link}});var f=[];for(var g in b)b.hasOwnProperty(g)&&f.push(b[g]);f.sort(function(a,b){return a.lang_raw.toLowerCase()>b.lang_raw.toLowerCase()?1:a.lang_raw.toLowerCase()<b.lang_raw.toLowerCase()?-1:0});for(var h=0;h<f.length;++h){var i=jQuery("#language-template").html(),g=f[h];i=i.replace("{{LANGUAGE}}",g.lang).replace("{{NAME}}",g.user).replace("{{SIZE}}",g.size).replace("{{LINK}}",g.link),i=jQuery(i),jQuery("#languages").append(i)}}var QUESTION_ID=101638,ANSWER_FILTER="!t)IWYnsLAZle2tQ3KqrVveCRJfxcRLe",COMMENT_FILTER="!)Q2B_A2kjfAiU78X(md6BoYk",OVERRIDE_USER=34718,answers=[],answers_hash,answer_ids,answer_page=1,more_answers=!0,comment_page;getAnswers();var SCORE_REG=/<h\d>\s*([^\n,<]*(?:<(?:[^\n>]*>[^\n<]*<\/[^\n>]*>)[^\n,<]*)*),.*?(\d+)(?=[^\n\d<>]*(?:<(?:s>[^\n<>]*<\/s>|[^\n<>]+>)[^\n\d<>]*)*<\/h\d>)/,OVERRIDE_REG=/^Override\s*header:\s*/i;
body{text-align:left!important}#answer-list,#language-list{padding:10px;width:400px;float:left}table thead{font-weight:800}table td{padding:5px}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><link rel="stylesheet" type="text/css" href="//cdn.sstatic.net/codegolf/all.css?v=83c949450c8b"><div id="language-list"> <h2>Shortest Solution by Language</h2> <table class="language-list"> <thead> <tr><td>Language</td><td>User</td><td>Score</td></tr></thead> <tbody id="languages"> </tbody> </table></div><div id="answer-list"> <h2>Leaderboard</h2> <table class="answer-list"> <thead> <tr><td></td><td>Author</td><td>Language</td><td>Size</td></tr></thead> <tbody id="answers"> </tbody> </table></div><table style="display: none"> <tbody id="answer-template"> <tr><td>{{PLACE}}</td><td>{{NAME}}</td><td>{{LANGUAGE}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table><table style="display: none"> <tbody id="language-template"> <tr><td>{{LANGUAGE}}</td><td>{{NAME}}</td><td>{{SIZE}}</td><td><a href="{{LINK}}">Link</a></td></tr></tbody></table>

\$\endgroup\$
27
  • 41
    \$\begingroup\$ @mbomb007 In C there are many things that are simply "undefined" behaviour. Any given interpreter is allowed to do whatever it wants in any situation. For all we know, gcc might order you a pizza if you try to overflow a signed integer on a rainy Tuesday, but will make a trout jump out of your screen on all other days. So you wouldn't really ever know if it's actually deterministic or not in any given implementation. \$\endgroup\$ Nov 30, 2016 at 20:44
  • 13
    \$\begingroup\$ @MartinEnder I'm not sure if that matters. We define languages here by their implementation, not by the specification (as languages without an implementation is not allowed) \$\endgroup\$ Nov 30, 2016 at 21:00
  • 8
    \$\begingroup\$ Note that undefined behaviour in C often leads to crashes, and crashes on UNIX and Linux lead to core files which contain the process ID inside them. That would seem to comply with the question as currently worded. \$\endgroup\$
    – user62131
    Nov 30, 2016 at 21:23
  • 5
    \$\begingroup\$ Unless I misunderstood, the question did not ask for code that takes advantage of undefined behavior. It asks for code that takes advantage of defined behavior to guarantee non-determinism. \$\endgroup\$
    – WGroleau
    Dec 1, 2016 at 3:30
  • 4
    \$\begingroup\$ @MartinEnder damn, missed my free GCC pizza yesterday. \$\endgroup\$ Dec 2, 2016 at 12:04

132 Answers 132

5
\$\begingroup\$

Mathematica, 6 bytes

Date[]

Pretty self-explanatory. (This function was superseded by DateList[] in later versions, but Date[] still runs.)

For 8 bytes one can also use Random[] (itself a legacy function).

\$\endgroup\$
3
  • 1
    \$\begingroup\$ For 10 characters, you can us $ProcessID. \$\endgroup\$ Dec 1, 2016 at 3:07
  • \$\begingroup\$ Aha, nice idea! The 7-byte $System is the shortest one I could find. \$\endgroup\$ Dec 1, 2016 at 9:25
  • \$\begingroup\$ Shorter: Now. \$\endgroup\$
    – alephalpha
    Aug 3, 2022 at 5:48
5
\$\begingroup\$

MATL, 1 byte

r

Uniform random number generator between 0 and 1. The seed is randomly set for each execution of the program. Try it online!

\$\endgroup\$
1
  • 3
    \$\begingroup\$ Same exact solution and explanation works in Pip. \$\endgroup\$
    – DLosc
    Dec 1, 2016 at 3:34
5
\$\begingroup\$

C89 with GCC/Clang, 20 bytes

The other C solution just segfaults every time when built with GCC or Clang. This, though.

main(n){puts(&n+1);}

Which looks like:

$ for _ in `seq 1 50`; do ./test_89; done
���z�U
�VW��U
�F��U
�v�f2V
��FV
���*=V
�6���U
�20wU
��
�+V
�6
   �U
��V�uU
�v��V
���K�U
��7�qU
�6S�jU
�&�WU
��wV
��6l�U
���U
�F�ߨU
�f���U
���s7V
�f��?V
��;B�U
�;��U
��GV
�� ��U
�vKV
�V?]wU
�����U
��.�U
�v"�XU
��uhpU
��LD�U
�����U
�6X�U
��M�.V
�69��U
��ԤV
���U
����U
�vx4.V
�֝+xU
�F��U
�֤BQV
��#�U
���1^U
����sU
��4�U
��AݗU

Quite a lot of unprintable junk but it's nondeterministic!

\$\endgroup\$
5
  • \$\begingroup\$ why "main(n){puts(&n+1);}" and not "main(n){puts(&n);}"? \$\endgroup\$
    – user58988
    Dec 2, 2016 at 22:23
  • \$\begingroup\$ @RosLuP Your second option, which seems obvious to the casual observer, gives the byte at the value of n (when n is 1, putsing its address gives 1, and when n is 2, putsing its address gives 2). Adding 1 to the address of n, which should point to a 4-byte wide int, gives a junk address with a junk value stored there with a very certain number of bytes until the next NUL byte. This behaviour is reproducible between GCC and Clang and completely beyond me. I think I'll go ask on StackOverflow. \$\endgroup\$
    – cat
    Dec 2, 2016 at 22:30
  • \$\begingroup\$ i read "puts(&n)" in this way: it give to puts the address of n, suppose that n=0x01020304 puts would print converted in chars 04 03 02 01 or reverse of that \$\endgroup\$
    – user58988
    Dec 2, 2016 at 22:41
  • 1
    \$\begingroup\$ Remember your n is still initialized with what's normally called argc which is 0 in your general test case, so with &n, puts gets a quite deterministic pointer to a '\0' byte resulting in an empty string (assuming pointer size == integer size and all that stuff). &n+1 however is the address of what's normally called argv (at least on ABIs that pass parameters on the stack in reverse order instead of registers, and with a stack that grows from high to low addresses), which, assuming ASLR, should be a different pointer every time. \$\endgroup\$ Dec 5, 2016 at 15:11
  • \$\begingroup\$ @GuntramBlohm You're right and that's very interesting, although for me pointers are 8 bytes and ints are 4 bytes. \$\endgroup\$
    – cat
    Dec 5, 2016 at 15:29
5
\$\begingroup\$

PHP, 12 bytes

<?=uniqid();

Outputs a unique ID 583f4da627ee3 based on the current time in microseconds.

\$\endgroup\$
4
  • \$\begingroup\$ <?=time(); <-- 10 bytes. \$\endgroup\$ Nov 30, 2016 at 23:23
  • \$\begingroup\$ @IsmaelMiguel uniqid() is 1'000'000 times more undetermined than time() ;) \$\endgroup\$
    – Mario
    Nov 30, 2016 at 23:40
  • \$\begingroup\$ I'm not saying the oposite. But proposing another answer. You're free to pick that one. \$\endgroup\$ Nov 30, 2016 at 23:45
  • \$\begingroup\$ @IsmaelMiguel someone else gave the same answer already... \$\endgroup\$
    – Mario
    Nov 30, 2016 at 23:46
5
\$\begingroup\$

INTERCAL, 0 2 bytes (0+2 for command flag)

Try it online! (I don't know if the TiO compiler reproduces this functionality correctly)

This is not a valid INTERCAL program, so attempting to run it gives Error 778 - UNEXPLAINED COMPILER BUG. However, when INTERCAL is run without the -b command flag, there is a small chance that it doesn't run and throws Error 774 - RANDOM COMPILER BUG. So if errors count as output for this challenge, then INTERCAL might actually be good for something!

INTERCAL, 17 bytes

DO %9 READ OUT #5

Try it online!

This is a more standard approach. It has 9% chance of printing V before terminating (with an error, but it gives the output, so who cares?)

\$\endgroup\$
4
  • \$\begingroup\$ Compiler flags have to be included in the byte count. See meta post. \$\endgroup\$
    – mbomb007
    Oct 6, 2017 at 16:59
  • 1
    \$\begingroup\$ @mbomb007 It doesn't use any compiler flags--that's the whole point. The "RANDOM COMPILER BUG" occurs when a program is run without the -b flag. \$\endgroup\$
    – KSmarts
    Oct 6, 2017 at 18:22
  • \$\begingroup\$ Any difference from the standard flags counts. So if removing the flag is the difference, that's a one-byte difference. \$\endgroup\$
    – mbomb007
    Oct 6, 2017 at 19:08
  • 3
    \$\begingroup\$ @mbomb007, although any sensible person would run the INTERCAL compiler with the -b option, a sensible person wouldn't be using INTERCAL in the first place. \$\endgroup\$
    – Mark
    Oct 6, 2017 at 22:07
5
\$\begingroup\$

Borland C on Windows, 12 bytes

m(){puts();}

I rewrote it because they said it's possible to use one function. The compiler doesn't check the argument so it compiles it; but puts sees an unknown address and begins to print what that address points to until it finds the byte 0x00. It would not work if that address was out of memory, but here it prints something.

\$\endgroup\$
8
  • \$\begingroup\$ This doesn't give nondeterministic output, it just segfaults every time. \$\endgroup\$
    – cat
    Dec 1, 2016 at 14:26
  • \$\begingroup\$ Alternatively, if you get something other than a segfault, what compiler ?? \$\endgroup\$
    – cat
    Dec 1, 2016 at 14:35
  • \$\begingroup\$ @cat it is one Borland C compiler + Windows7 Os. In how I see: above code gets the address top of stack (where in this case there is the address to return in function main() )and read from that address inside main space code... So it depend to compiler output. But I don't know 100% ... It is possible code space is not readable in your Os and from this => seg fault \$\endgroup\$
    – user58988
    Dec 1, 2016 at 14:53
  • 1
    \$\begingroup\$ @RosLuP: It would just print whatever garbage was in stack memory (or in the second arg-passing register, for x86-64 and most RISC calling conventions that pass the first few args in registers). It would not print the address of the stack. In x86-64, it would be somewhat likely to print argv, since the compiler would probably call printf with main's second arg still in that register. That's exactly what happens with gcc6.2 targeting Linux: See the source+asm on the Godbolt compiler explorer: main doesn't touch RSI before call printf. \$\endgroup\$ Dec 6, 2016 at 14:42
  • 1
    \$\begingroup\$ @RosLuP: argv is on the stack, but not at the very top. Its address is affected by stack ASLR, though, so that works. This would work less well with -m32. You'd probably always get zero, since main has to keep the stack aligned so the stack slot above the format string may be fresh stack memory that has never been touched (and is probably always zero, since the kernel avoids info leaks by zeroing pages instead of giving user-space pages full of old data). \$\endgroup\$ Dec 6, 2016 at 14:47
4
\$\begingroup\$

Japt, 1 byte

Ð

Test it online!

How it works

When Japt is compiled to JavaScript, it replaces Ð with new Date(. The close-paren is automatically added by the interpreter, and the result of the last expression is automatically printed, so this prints the current date/time.

An alternate solution would be Mr, which uses Math.random. Japt also has the variable K set to Date (no clue why I did that, it's almost entirely useless), which I guess makes K a valid function entry.

\$\endgroup\$
4
\$\begingroup\$

Jelly, 2 bytes

9X

Idk if this works. I've never done anything in jelly before, and I'm on my phone right now.

Int from 0 to 9.

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Prints 09 every time for me. 9X works though \$\endgroup\$ Nov 30, 2016 at 20:57
  • \$\begingroup\$ @ETHproductions Thanks. That was my original answer, I edited because I thought it didn't work. :/ I can't test things on my phone, so I had to go with what looked right from never using the language before. \$\endgroup\$
    – Pavel
    Nov 30, 2016 at 21:28
  • \$\begingroup\$ It's [1, 9], not [0, 9]. Also, you can do ⁹X to do [1, 256] for the same bytecount. \$\endgroup\$ Dec 2, 2016 at 21:29
4
\$\begingroup\$

C#, 36 22 bytes

a=()=>a.GetHashCode();

Yay.

\$\endgroup\$
7
  • \$\begingroup\$ Why the braces and the return? Just make it ()=>Environment.TickCount; \$\endgroup\$
    – Dennis_E
    Dec 1, 2016 at 15:18
  • \$\begingroup\$ @Dennis_E Yeah I didn't know you could do it like that for one liners, thanks! \$\endgroup\$
    – Yodle
    Dec 1, 2016 at 15:37
  • 1
    \$\begingroup\$ You beat the javas :[ Have an upvote. (verbose language users like us gotta stick together) \$\endgroup\$
    – Poke
    Dec 1, 2016 at 15:39
  • 1
    \$\begingroup\$ @Poke :D I'm trying to figure out if there's an even shorter way, but so far most of the other things for the function don't return different things each run. Although doing a.Target returns nondeterministic+<>c__DisplayClass0_0 which has 0_0 in it, which is cool. \$\endgroup\$
    – Yodle
    Dec 1, 2016 at 15:41
  • \$\begingroup\$ Wouldn't DateTime.Now also fit the requirements? It produces different output at different times and is shorter than Environment.TickCOunt() or a.GetHashCode(). \$\endgroup\$
    – Snowfire
    Dec 2, 2016 at 7:59
4
\$\begingroup\$

Java2K, 9 bytes

I don't recommend trying to write an approximation of a deterministic program in this language.

11 6/*/_\

This function has a 90% chance to return 1, otherwise it will return a random number. 11 6 is the name of the division function. A function is called like <name>/<arg0>/<arg1>\. Basicaly every function returns the "correct" result 90% of the time, or else a random number.

The token * is replaced by a random number, say 203. The _ token is replaced by the previous argument, i.e. 203. So, this function will divide 203 by 203 which has (see below) a 90% chance of returning 1.

\$\endgroup\$
4
\$\begingroup\$

C++, 26 19 Bytes

std::cout<<new int;

Returns a different memory address each time it's run

\$\endgroup\$
3
  • \$\begingroup\$ What does the * do? \$\endgroup\$
    – Pavel
    Dec 2, 2016 at 8:21
  • \$\begingroup\$ @Pavel int* means a pointer to an integer. \$\endgroup\$
    – mbomb007
    Dec 2, 2016 at 14:39
  • \$\begingroup\$ Isn't this just a snippet? A program would need a main function... \$\endgroup\$
    – univalence
    Mar 5, 2017 at 12:56
4
\$\begingroup\$

Minecraft, 2 bytes

/r

Must be written into chat when playing on multiplayer servers. Will throw an error if you hadn't messaged (/tell) anybody recently. Otherwise, shows both players a random message.

Note: I'm not really sure if this is a legit solution :)

Minecraft, 4 bytes (+2 blocks)

help

Ties with another Minecraft answer from @Pavel

This is based on Minecraft's Easter egg:

When you type help or /help in a Command block, a random message starting with "Searge says:" is displayed, for example "Searge says: Ask for help on twitter". Here is a full list of messages.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ /r is not a Minecraft command. It only exists on servers which implement it, usually with Bukkit/Spigot/Paper plugins. \$\endgroup\$ Oct 18, 2020 at 16:20
4
\$\begingroup\$

C, 5 bytes

main;

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Julia, 4 bytes

rand

Try it online!

time

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Knight, 2 bytes

`R

Try it online!

`   Run as a shell command:
R   A random integer

The non-deterministic output is to stderr. The C interpreter implicitly converts the number to string and tries to run it as a shell command. With very high chance (if not 100%), such a command or executable does not exist in PATH, and an error message that looks like sh: <some random number>: command not found is printed to stderr.

This is shorter than the straightforward O R which prints the random number to stdout.

\$\endgroup\$
3
\$\begingroup\$

x86/amd64 machine language (Pentium or higher), 3 bytes

0:       0f 31                   rdtsc  
2:       c3                      retq

This uses the RDTSC instruction that may not be available on older processors. To test, try the following C program on Linux x86/amd64

#include <stdio.h>
int main(){
  for( int i = 0; i < 10; i++ ) {
    printf( "%d\n", ((int(*)())"\x0f\x31\xc3")() );
  }
}

Or try it on Ideone.

\$\endgroup\$
1
  • 1
    \$\begingroup\$ To clarify how the value is returned; RDTSC loads the most significant 32 bits into edx, and the least significant 32 bits into eax. The convention for returns in this matchine code is that an int-sized return value is stored in eax, so the function's return value is suitably nondeterministic. \$\endgroup\$
    – user62131
    Nov 30, 2016 at 21:38
3
\$\begingroup\$

Cubix, 3 bytes (non-competing)

@OD

Outputs a random positive number of zeroes. Non-competing because I added D just a few minutes ago. Before it was added, there was absolutely no way to randomize anything in Cubix.

Test it online!

How it works

Before the code is run, it's padded with no-ops . and formed into a cube net. Here's what the net for this particular program looks like:

  @
O D . .
  .

Now the code is run, starting on the left-most face and heading to the right.

First, O outputs the top item on the stack as a number. At the beginning of the program, the stack is an infinite fount of zeroes, so this prints 0.

Next, D sends the IP (instruction pointer) in a random direction: possibly up to the @ which ends the program, and possibly back to the O which prints another 0. This continues until whenever the IP hits the @.

@DO and OD@ also work, each printing zero or more zeroes.

\$\endgroup\$
3
\$\begingroup\$

PHP, 10 Byte

<?=time();

This prints the current Unix time which – of course – isn't random but changes when you call the program several times. The output I just got is 1480548602.

\$\endgroup\$
1
  • \$\begingroup\$ Welcome to PPCG! Nice answer, by the way. \$\endgroup\$ Dec 2, 2016 at 21:33
3
\$\begingroup\$

APL, 2 bytes

?9

This uses APL's roll (?) operator. The program prints a pseudo-random number between 1 and 9.

\$\endgroup\$
3
  • \$\begingroup\$ Also works in J. \$\endgroup\$ Dec 1, 2016 at 11:27
  • 2
    \$\begingroup\$ Looks deterministic to me, except if ⎕RL≡⍬2 or after executing ⎕RL←0. A safer bet is ⎕TS or ⎕AI. \$\endgroup\$
    – Adám
    Dec 5, 2016 at 23:43
  • 1
    \$\begingroup\$ Update: Your code has now become non-deterministic, as the default has become to ask the OS for a random number. Try it online! \$\endgroup\$
    – Adám
    Feb 2, 2018 at 9:30
3
\$\begingroup\$

PHP 10 bytes

<?=rand();

Prints a random number between 0 and getrandmax(). Which may vary based on what computer it's run on.

\$\endgroup\$
3
\$\begingroup\$

Lua, 9 bytes

print({})

Outputs the internal address of the Lua table object:

table: 0x2370ab0

If you're running from the interactive interpreter, you can use just 3 bytes:

> ={}
table: 0x23719c0
\$\endgroup\$
3
\$\begingroup\$

C, 19

f(a,b){putchar(b);}

In C the second declared variable in a function's arguments is random based on memory. Assuming the arguments were not actually declared, i.e. you just called f();.

So this function can be simply called with no input-arguments. That is, just: f(); and the output will be a random character.

Try it online! (Note: output might be an unprintable character, run the program a few times to see variation, you should get more printable characters than non-printable characters)

\$\endgroup\$
3
\$\begingroup\$

C (gcc), 18 bytes

f(){printf("%d");}

This one may be difficult to test since TIO will give you a cache hit. Just add some whitespace and try it again.

Try it online!

\$\endgroup\$
3
\$\begingroup\$

Retina, 3 bytes

?&`

Try it online!

Yay, Retina can finally compete in challenges that require randomness! \o/

Explanation

?& is a compound stage which executes its child stage with a 50% probability. That child stage is itself a Count stage, which counts the number of empty matches in the empty input (1). So if the child stage is run, the result is 1, if it isn't, then the empty input remains unchanged and we get an empty string.

A fun alternative for the same byte count would be:

?+`

Try it online!

This one uses a random loop (which uses a coin toss before each iteration to decide whether to continue). This one has a 50% chance of printing an empty string, 25% of printing 1 (one iteration) and a 25% chance of printing 2 (two or more iterations).

\$\endgroup\$
3
\$\begingroup\$

Kotlin script (.kts), 12 bytes

print(Any())

Any is a compile-time time wrapper for Object, default toString() of any object is class name@hashcode. Probably the shortest solution that runs on the JVM.

If .kts is cheating:

18 bytes: fun a()="${Any()}" (forgot functions are a valid answer)

38 bytes: fun main(a:Array<String>)=print(Any())

\$\endgroup\$
4
  • \$\begingroup\$ Trying this on my OpenJDK JRE with a freshly downloaded kotlinc 1.0.5-2 repeatably outputs java.lang.Object@5bc1903. Doesn't seem too nondeterministic? \$\endgroup\$ Dec 4, 2016 at 19:31
  • \$\begingroup\$ Hm, didn't think of that - is it still a valid answer if it's different on every machine? Default hashcode impl. uses the internal memory address of the object IIRC. \$\endgroup\$
    – F. George
    Dec 5, 2016 at 4:23
  • \$\begingroup\$ Just out of curiosity, what does it output on your machine? \$\endgroup\$ Dec 5, 2016 at 18:02
  • \$\begingroup\$ java.lang.Object@21471ca8 but it seems to vary between IDE restarts and even how I name the source file. Makes sense, as the hashcode is literally just the memory address of the object. \$\endgroup\$
    – F. George
    Dec 5, 2016 at 20:29
3
\$\begingroup\$

Wumpus, 4 bytes

UFO@

Try it online!

Outputs a uniformly random choice of 2, 5 or 8.

Explanation

Apart from a regular stack, Wumpus also has 20 registers which are arranged around the faces of an icosahedron. You can imagine the icosahedron as a d20 resting on a table: the face touching the table is the "active" face (i.e. the register that can currently be interacted with). Also, every face has an index from 1 to 20. Initially, the active face is 1 and the three adjacent faces are 2, 5 and 8 (see the README in the Wumpus repo for the full net).

U   Randomly tip the icosahedron onto one of the three adjacent faces, i.e.
    change the active faces to either 2, 5 or 8.
F   Push the index of the currently active face.
O   Output as a decimal integer.
@   Terminate the program.

Alternatively:

DFO@

This one completely randomises the orientation of the icosahedron, so it prints a random number from 1 to 20, inclusive.

\$\endgroup\$
1
  • 8
    \$\begingroup\$ Little-known fact: This works in Mathematica too! The code reads as UFO at and so it grabs the location of the nearest UFO and prints it, something which is obviously non-deterministic. \$\endgroup\$ Feb 12, 2018 at 3:45
3
\$\begingroup\$

Lost, 7 bytes

@%"
///

Try it online!

Since Lost starts the pointer in a random place facing a random direction, it's refreshing not making the output deterministic for once. I think this is the optimal solution since you need the @ and % in order to end the program, the " to push stuff to the stack at random, and the /// to avoid infinite loops.

\$\endgroup\$
1
  • 2
    \$\begingroup\$ The challenge does not require the program to halt. Thus @% alone will work. This either loops forever outputting nothing or halts and outputs a newline. \$\endgroup\$
    – Wheat Wizard
    Aug 23, 2018 at 3:20
3
\$\begingroup\$

Intercept 1.1, 37 bytes


Prints newlines to the terminal. You'll probably have to leave this one overnight, as there is a delay of 5-15 minutes (it's random!) between each newline. Note that while you can determine what is being printed, you can't determine how often it's being printed.

Input is marked with a >>. The command prompt is the empty >>.

Assuming a system with only TZ_INFECT installed...


>> malware bm bitminer
creating bm (bitminer)
... wait a minute ...

finished creating bm (bitminer)
>> software install 1
Success
bm installed

...wait...
(newlines)








>>

Note that I'm using a custom client that prefixes [BROADCAST] to all broadcast events.

How???

TZ_INFECT is Intercept's malware generator. We can generate a bitminer by running malware <name> bitminer. This will create a new piece of software with the given name of type bitminer

We then install the bitminer: software install <index>, where index is the 0-based index of the software as given by software list. This is why I made sure the only piece of software on the system was the malware generator. This places our new bitminer at index 1.

There is a bug in the implementation of bitminer that results in an empty broadcast event to be sent to the client whenever bits are generated. Since bits are generated at random intervals, the broadcasts are sent at random intervals. Voilia, non-deterministic output!

Note: the newlines are harder to detect using the official game client, but you can see them by running a command and watching the newlines slowly push the resulting output off the screen.

Screenshots coming in a few hours while I wait for the output.
After 20 minutes (the [BROADCAST] is a newline): Intercept

After 2 days:

Intercept

\$\endgroup\$
3
\$\begingroup\$

Taxi, 222 220 218 bytes

Go to Heisenberg's:w 1 r, 3 r, 1 l.Pickup a passenger going to The Babelfishery.Go to The Babelfishery:s 1 r, 1 l.Pickup a passenger going to Post Office.Go to Post Office:n, 1 l, 1 r.Go to Taxi Garage:n 1 r, 1 l, 1 r.

Prints a random number. The spec does not give any details on what number should be. The last statement can be omitted (producing 184 bytes) if you don't mind the code giving an error after printing the output.

Try it online!

\$\endgroup\$
1
  • \$\begingroup\$ Can you link to the language, since this is not a well-known one? \$\endgroup\$
    – mbomb007
    Jul 29, 2017 at 3:30
3
\$\begingroup\$

PARI/GP, 2 bytes

\s

Attempt This Online!

\s prints the state of the PARI stack and heap, which is non-deterministic.

Example output:

 Top : 7fadf82bd000   Bottom : 7fadf7b1be00   Current stack : 7fadf82bcfb8
 Used :                         9  long words  (0 K)
 Available :                    999991  long words  (7808 K)
 Occupation of the PARI stack :   0.00 percent
 1 objects on heap occupy 9 long words

 10 variable names used (10 user + 0 private) out of 65535
\$\endgroup\$

Your Answer

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

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