83
3
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>
What about undefined behaviour? – Martin Ender – 2016-11-30T20:32:42.280
@MartinEnder Undefined based on the implementation, or based on where it's run? – mbomb007 – 2016-11-30T20:38:01.897
33@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. – Martin Ender – 2016-11-30T20:44:34.687
12@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) – Nathan Merrill – 2016-11-30T21:00:51.950
2@MartinEnder Yeah, I agree with Nathan. – mbomb007 – 2016-11-30T21:01:23.167
@NathanMerrill My point was that for undefined behaviour we can't really tell to what degree it's non-deterministic unless we reverse engineer the implementation. Undefined behaviour is basically the definition of non-deterministic, but any given implementation may for any given set of circumstances decide to always to the same thing when you run the code. – Martin Ender – 2016-11-30T21:04:38.977
1That's true. Non-deterministic is defined in the specification, not the implementation (because you can't detect determinism in a black box). – Nathan Merrill – 2016-11-30T21:06:08.693
1@MartinEnder Any non-determinism defined (or not defined?) in the spec is not referring to a single implementation, but to all of them. It's simply trying to say don't rely on it. But for an answer, I'd say you would need to look at the implementation and prove that the answer is not always the same, especially if I were to run it and it's always the same for my implementation I'm running that's the same as yours. – mbomb007 – 2016-11-30T21:08:14.250
@mbomb007 Undefined behaviour is not the same as implementation defined behaviour. Undefined behaviour does allow to do anything under any circumstances (and is often the cause of irreproducible bugs). And always returning the same thing is not a reliable measure of randomness.
– Martin Ender – 2016-11-30T21:12:26.677@MartinEnder If I can run it and see different results for the same implementation, I'll allow it. – mbomb007 – 2016-11-30T21:15:31.520
7Note 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. – None – 2016-11-30T21:23:14.857
1@ais523 Stderr is not valid by consensus. Consensus requires something like 3/4 upvotes out of total votes. Also, you can't say "often". It has to be in the code, guaranteed to output the required non-deterministic result. – mbomb007 – 2016-11-30T21:28:43.257
Consensus says that output to a file is valid. The crash dump is a file, with the filename
core
. – None – 2016-11-30T21:29:47.297@ais523 If I can't validate the answer, it's not valid. – mbomb007 – 2016-11-30T21:30:35.590
1So if the seed of the random generator is set to a different (random) value each time by default, can a random number generator be used? – Luis Mendo – 2016-11-30T21:48:58.427
@LuisMendo Yes, if the randomness that determines the seed is not deterministic. – mbomb007 – 2016-11-30T22:06:49.160
5Unless 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. – WGroleau – 2016-12-01T03:30:58.510
1What do you mean by "Your code may not take non-empty input."? Surely a program that doesn't input anything anywhere can't be nondeterministic. – Angs – 2016-12-01T17:33:36.033
@Angs http://meta.codegolf.stackexchange.com/a/7172/34718
– mbomb007 – 2016-12-01T17:41:19.8002@MartinEnder damn, missed my free GCC pizza yesterday. – Mindwin – 2016-12-02T12:04:36.233
Now that there are a bunch of function answers, I'd just like to clarify: Are function entries allowed? – ETHproductions – 2016-12-02T22:28:52.923
@ETHproductions My earlier comment appears to have been deleted. Yes, I was intentionally vague in order to allow the default (full program or function). – mbomb007 – 2016-12-03T04:51:07.000
For C with undefined behaviour,
main=0;
would produce a non-deterministic core-file in 7-bytes. – Mark K Cowan – 2016-12-06T15:34:59.510You say that the program does bot need to halt. Would a program that either halts or loops forever with a 50% chance of each be non-deterministic? – Post Rock Garf Hunter – 2018-08-23T03:22:49.447
@WW I don't think that would count as "output". The not needing to halt means that output should happen in either case. The output could be empty string for one of them. – mbomb007 – 2018-08-23T18:03:28.080
Can we use environment variables as a source of non-determinism? – Esolanging Fruit – 2019-02-18T07:24:27.140
@EsolangingFruit No, because those are generally either defaults or set by the user. They don't really change, and consecutive runs of your program would always output the same result. – mbomb007 – 2019-02-18T15:00:05.953