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>
-
42\$\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\$Martin Ender– Martin Ender2016年11月30日 20:44:34 +00:00Commented 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\$Nathan Merrill– Nathan Merrill2016年11月30日 21:00:51 +00:00Commented Nov 30, 2016 at 21:00
-
9\$\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– user621312016年11月30日 21:23:14 +00:00Commented 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– WGroleau2016年12月01日 03:30:58 +00:00Commented Dec 1, 2016 at 3:30
-
5\$\begingroup\$ @MartinEnder damn, missed my free GCC pizza yesterday. \$\endgroup\$Mindwin Remember Monica– Mindwin Remember Monica2016年12月02日 12:04:36 +00:00Commented Dec 2, 2016 at 12:04
136 Answers 136
WinDbg, 1 byte
#
Wow! Never expected a 1 byte solution from WinDbg!
#
searches for a disassembly pattern, but since there's no parameters, it looks to just return the next assembly instruction in whatever dump/process you're attached to. Not sure the logic for setting the initial address, but it does.
Sample output:
0:000> #
Search address set to 75959556
user32!NtUserGetMessage+0xc
75959556 c21000 ret 10h
0:000> #
user32!NtUserGetMessage+0xf
75959559 90 nop
0:000> #
user32!NtUserMessageCall
7595955a 90 nop
0:000> #
user32!NtUserMessageCall+0x1
7595955b 90 nop
0:000> #
user32!NtUserMessageCall+0x2
7595955c 90 nop
0:000> #
user32!NtUserMessageCall+0x3
7595955d 90 nop
0:000> #
user32!GetMessageW
7595955e 8bff mov edi,edi
0:000> #
user32!GetMessageW+0x2
75959560 55 push ebp
0:000> #
user32!GetMessageW+0x3
75959561 8bec mov ebp,esp
0:000> #
user32!GetMessageW+0x5
75959563 8b5510 mov edx,dword ptr [ebp+10h]
-
12\$\begingroup\$ One of the cooler answers here, this should win over a "Current Date" solution in my opinion. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2016年11月30日 21:39:33 +00:00Commented Nov 30, 2016 at 21:39
-
\$\begingroup\$ that above it seems the disassebly, step by step of the begin of some function in 386 intel cpu assembly \$\endgroup\$user58988– user589882016年12月02日 22:05:33 +00:00Commented Dec 2, 2016 at 22:05
MATLAB, 3 bytes
why
why
provides answers to almost any question. A few examples:
why
The programmer suggested it.
why
To fool the tall good and smart system manager.
why
You insisted on it.
why
How should I know?
This is shorter than any rand
function I can think of.
-
51\$\begingroup\$ MATLAB has a builtin for this? Why? \$\endgroup\$ETHproductions– ETHproductions2016年12月03日 20:46:18 +00:00Commented Dec 3, 2016 at 20:46
-
89\$\begingroup\$ @ETHproductions The programmer suggested it \$\endgroup\$Eddie Curtis– Eddie Curtis2016年12月05日 16:04:06 +00:00Commented Dec 5, 2016 at 16:04
Java 7, (削除) 33 (削除ここまで) (削除) 30 (削除ここまで) 27 bytes
int a(){return hashCode();}
Because Java.
-
56\$\begingroup\$ Because Java. Probably the best explanation of Java ever. \$\endgroup\$F. George– F. George2016年11月30日 21:35:54 +00:00Commented Nov 30, 2016 at 21:35
-
5\$\begingroup\$ @carusocomputing I had
toString
in a previous version but then the return type isString
which is longer thanint
. Save the bytes! :] \$\endgroup\$Poke– Poke2016年11月30日 22:17:06 +00:00Commented Nov 30, 2016 at 22:17 -
12\$\begingroup\$ Is this valid as a function?
hashCode()
is an abbreviation here forthis.hashCode()
, so it'd only work as an instance method, not a static method. In that case, you'd need additional code to create an object in the caller. That's relevant here because it's the code to create an object that's responsible for the nondeterminism. \$\endgroup\$user62131– user621312016年12月01日 01:08:04 +00:00Commented Dec 1, 2016 at 1:08 -
18\$\begingroup\$ In Java 8:
()->hashCode()
for 14 bytes. Just sayin' ;) \$\endgroup\$Olivier Grégoire– Olivier Grégoire2016年12月01日 16:58:12 +00:00Commented Dec 1, 2016 at 16:58 -
4
huh?, 0 bytes
An empty program still produces output. The last lines of the Python interpreter that are executed:
print "..."
f = open('Notes.txt', 'w')
f.write(time.strftime("%c") + " - The user tried to give me commands again. I still have no idea what they are talking about...\n")
At the end of a program, the Python interpreter will print ...
, then it will create/open a text file called Notes.txt
and write a string which contains the current time in front.
-
6\$\begingroup\$ Of all the things I thought people would use on my GitHub, I didn't think a joke language interpreter would be one :P \$\endgroup\$Kade– Kade2016年12月02日 18:41:39 +00:00Commented Dec 2, 2016 at 18:41
-
\$\begingroup\$ @Kade I was actually going to use it to answer another question as well (shortest code to play a sound, with only a single byte) -- but the question is now closed. \$\endgroup\$mbomb007– mbomb0072016年12月02日 19:36:59 +00:00Commented Dec 2, 2016 at 19:36
R, 1 byte
t
Outputs the function's source code and a memory pointer address which changes with every (re-)start of R.
Minecraft, (削除) 5 (削除ここまで) 4 bytes
op 8
Used by typing into a server's console or a command block and giving it power. Can be run from the chat interface by prepending a /
.
Usually this does nothing, but if there's a player with the username "8" on the server, they will be given operator permissions. Note that while Minecraft normally requires usernames to be 3 characters long, some accounts with shorter name lengths were created before this restriction.
The version that can be demonstrated to be non-deterministic without one of these usernames or risk of giving a user operator permissions is here:
me @r
Displays a message to everyone, the message being the username of a random player. The op
command only takes a string literal, not any script that returns a string.
The me
command wouldn't really work for the first example, it would display "<your-username> 8"
. When run from a command block, it wouldn't be deterministic since all command blocks have the same "username" but running it from the chat interface would require the /
for one extra byte.
-
\$\begingroup\$ So
/
is not included in the byte count? \$\endgroup\$Esolanging Fruit– Esolanging Fruit2016年12月01日 05:36:10 +00:00Commented Dec 1, 2016 at 5:36 -
13\$\begingroup\$ @Challenger5 The slash is optional in command blocks, not allowed in the server console, and mandatory in the chat interface. \$\endgroup\$Pavel– Pavel2016年12月01日 05:47:54 +00:00Commented Dec 1, 2016 at 5:47
-
2\$\begingroup\$ Actually, it should be 4 bytes + 2 blocks (Command block and redstone source), or 6 blytes \$\endgroup\$user60199– user601992016年12月02日 16:38:07 +00:00Commented Dec 2, 2016 at 16:38
-
3\$\begingroup\$ @RudolphJelinek You can run it from the console without command blocks. \$\endgroup\$Pavel– Pavel2016年12月02日 17:10:56 +00:00Commented Dec 2, 2016 at 17:10
-
1\$\begingroup\$ Another non-deterministic option would be
help
in a command block, which is 4 bytes (and doesn't require pirated games). \$\endgroup\$Pokechu22– Pokechu222016年12月06日 19:03:10 +00:00Commented Dec 6, 2016 at 19:03
Labyrinth, 5 bytes
v
!
@
Either prints 0
or nothing (50% chance each).
There is a very specific case in which Labyrinth exhibits random behaviour:
- There must be a wall in front of the instruction pointer and behind it.
- There must be a non-wall left and right of the instruction pointer.
- The current top of the stack must be zero.
If all of those conditions are met, the direction the IP moves in is chosen (uniformly) randomly. The cherry on top is that those three conditions are impossible to meet in regular control flow, which means unless you modify the source code at runtime.
(This may seem a bit arbitrary, but it's actually the most consistent behaviour I could find for these conditions, since normally the direction of the IP always depends on the previous direction, its neighbours, and the sign of the top of the stack, and this seemed like an elegant way to include a source of randomness in the language.)
With the help of the source code rotation instructions (<^>v
) it's possible to bring the IP into this situation. One such example is seen at the top. The IP initially points east and starts at the top. The v
rotates the current column so that we get:
@
v
!
The IP moves along with this rotation so that it's still on the v
, pointing east. All the conditions are fulfilled now, so the IP will either go up or down randomly. If it goes up, the program terminates immediately. If it goes down, it prints a zero, rotates the column again, and then terminates.
There are three other programs making use of this (one which also prints 0
, one which prints 00
and one which prints 000
):
v
@
!
"
>@!
"
>!@
(Actually there are a lot more than three other programs, because you could also use .
instead of !
to print null bytes, or replace that "
with a large variety of commands, but I believe they all work essentially the same.)
Befunge (-93 and -98), 3 bytes
?.@
The ?
sends execution in a random direction. If it goes up or down, it loops back to the ?
and rerolls. If it goes left, the program wraps round to the @
and thus exits without printing anything. If it goes right, it prints 0
(the output produced by .
when the stack is empty) and then exits on the @
.
-
\$\begingroup\$ This isn't guaranteed to terminate, but I was going to post this exactly so +1 \$\endgroup\$Daniel– Daniel2016年12月01日 03:59:55 +00:00Commented Dec 1, 2016 at 3:59
-
\$\begingroup\$ You may want to change the comma to a period to output 0 as a number instead of a null character (ASCII value 0). +1 \$\endgroup\$MercyBeaucou– MercyBeaucou2016年12月01日 04:19:38 +00:00Commented Dec 1, 2016 at 4:19
-
\$\begingroup\$ It was
.
in the program I was using for testing, but somehow became a,
when I copied it onto PPCG. Fixed. \$\endgroup\$user62131– user621312016年12月01日 05:04:20 +00:00Commented Dec 1, 2016 at 5:04 -
9\$\begingroup\$ @Dopapp it terminates almost surely though, with probability 1. I'd take that as a guarantee ;) \$\endgroup\$Oliphaunt– Oliphaunt2016年12月01日 09:17:19 +00:00Commented Dec 1, 2016 at 9:17
-
\$\begingroup\$ @JamesHolderness I think you can do it in 1. Not sure if this counts though... \$\endgroup\$MercyBeaucou– MercyBeaucou2016年12月06日 18:18:25 +00:00Commented Dec 6, 2016 at 18:18
sh + procps, 1 byte
w
This is the shortest solution I'm aware of that works via calling into external executables. procps
is the responsible package for reporting information about the current system state (ps
and friends), and is installed on most Linux distributions by default; w
is the shortest-named command in it, and returns information about the logged-in users, but also some nondeterministic information like uptime.
Inform 7, 6 bytes
x is y
This isn't a valid Inform 7 program, since neither "x" nor "y" has been defined. So this throws an error.
However, some of Inform 7's error messages—including this one—are randomized. So the text it prints is technically non-deterministic.
A few possible outputs include:
Problem. The sentence 'x is y' appears to say two things are the same - I am reading 'x' and 'y' as two different things, and therefore it makes no sense to say that one is the other: it would be like saying that 'Adams is Jefferson'. It would be all right if the second thing were the name of a kind, perhaps with properties: for instance 'Virginia is a lighted room' says that something called Virginia exists and that it is a 'room', which is a kind I know about, combined with a property called 'lighted' which I also know about.
Problem. The sentence 'x is y' appears to say two things are the same - I am reading 'x' and 'y' as two different things, and therefore it makes no sense to say that one is the other: it would be like saying that 'Adam is Eve'. It would be all right if the second thing were the name of a kind, perhaps with properties: for instance 'Land of Nod is a lighted room' says that something called Land of Nod exists and that it is a 'room', which is a kind I know about, combined with a property called 'lighted' which I also know about.
Problem. The sentence 'x is y' appears to say two things are the same - I am reading 'x' and 'y' as two different things, and therefore it makes no sense to say that one is the other: it would be like saying that 'Clark Kent is Lex Luthor'. It would be all right if the second thing were the name of a kind, perhaps with properties: for instance 'Metropolis is a lighted room' says that something called Metropolis exists and that it is a 'room', which is a kind I know about, combined with a property called 'lighted' which I also know about.
Problem. The sentence 'x is y' appears to say two things are the same - I am reading 'x' and 'y' as two different things, and therefore it makes no sense to say that one is the other: it would be like saying that 'Aeschylus is Euripides'. It would be all right if the second thing were the name of a kind, perhaps with properties: for instance 'Underworld is a lighted room' says that something called Underworld exists and that it is a 'room', which is a kind I know about, combined with a property called 'lighted' which I also know about.
-
9\$\begingroup\$ Even the error messages are verbose! \$\endgroup\$Destructible Lemon– Destructible Lemon2016年12月01日 22:15:05 +00:00Commented Dec 1, 2016 at 22:15
JavaScript, 4 bytes
Date
A function which returns the current date/time. I think this is the shortest it will get...
Explanation
Since this seems to be causing a lot of confusion as to why it's valid, I'll try to explain.
In JavaScript, a function entry is valid if it can be assigned to a variable and called like a function. For example, this function is a valid entry:
function(){return Date()}
Because it is a function that can be assigned to a variable like so:
f=function(){return Date()}
And then run with f()
as many times as necessary. Each time, it returns the current date/time string, which has been ruled non-deterministic by the OP.
This ES6 arrow function is also valid:
_=>Date()
It can be assigned with f=_=>Date()
, then run with f()
like the other one.
Now, here's another valid entry:
Date
Why? Because just like the other two entries, it can be assigned with f=Date
and then called with f()
, returning exactly the same thing as the other two. Try it:
var f = Date
<button onclick="console.log(f())">Run</button>
-
7\$\begingroup\$ @milk
Date
is a function which, when called with no inputs, produces the current date/time._=>Date()
is a clearly valid entry which does exactly the same thing, soDate
is a valid entry. \$\endgroup\$ETHproductions– ETHproductions2016年11月30日 21:06:15 +00:00Commented Nov 30, 2016 at 21:06 -
3\$\begingroup\$ Typing
Date
to the JavaScript console of my browser produces deterministic output, it always produces this:function Date() { [native code] }
. You probably meanDate()
, 6 bytes. \$\endgroup\$pts– pts2016年12月02日 17:51:03 +00:00Commented Dec 2, 2016 at 17:51 -
2\$\begingroup\$ @pts That would be a snippet, not a function or even a full program. This is a function entry, i.e. you call the function to run it. It's exactly the same as
_=>Date()
. \$\endgroup\$ETHproductions– ETHproductions2016年12月02日 17:52:47 +00:00Commented Dec 2, 2016 at 17:52 -
4\$\begingroup\$ @RosLuP
Date
in JavaScript returns a function, just liken=>n
. I don't believemalloc(8)
returns a function, so it's either a full program or a snippet. \$\endgroup\$ETHproductions– ETHproductions2016年12月02日 22:01:32 +00:00Commented Dec 2, 2016 at 22:01 -
3\$\begingroup\$ @suriv This is a function entry, which have been allowed by the OP. See explanation for more info. \$\endgroup\$ETHproductions– ETHproductions2016年12月04日 20:42:39 +00:00Commented Dec 4, 2016 at 20:42
Haskell, 49 bytes
import Unsafe.Coerce
main=putStr[unsafeCoerce(+)]
Unfortunately this seems to always outputs the same thing on TIO (I don't quite know why, but I have some guesses), so if you want to try it you should run it locally.
I've only tested this on Linux and OSX. I make no guarentees about Windows. If you are having trouble getting it to work on Linux I would be interested to know.
Explanation
What is unsafeCoerce
?
This answer relies on unsafeCoerce
. This function is a bit of magic, so first we are going to have to do a quick intro to Haskell's type system.
Haskell has something called type erasure. This means that the compiler checks all the types to make sure that nothing in the program is called on the wrong type. Once it is done with that there is no need to actually store type information any more, so Haskell erases it. So at runtime all data is the same and just relies on the fact that we already checked all the types before hand so nothing goes horribly wrong.
unsafeCoerce
is a function that lies to the type checker. It tells it that it can take any input and produce any output; it's type is
forall a. forall b. a -> b
But it does nothing, it gives back the input exactly as it got it. So if you take a string like "abcd"
and coerce it to an int using unsafeCoerce
the result will be whatever the representation of "abcd"
in memory is when interpreted as an integer.
How are functions represented in memory?
In Haskell, all complex objects including functions are internally represented as a pointer. This is a number in binary that "points" to a specific location in memory. This is because we want to pass these values by reference, since copying the whole thing is expensive, and since Haskell disallows mutation we can with no problem.
However simple objects like ints are passed by value since they are so small that copying them is about as expensive as copying a pointer would be. So they are not represented as pointers but rather just whatever structure they are.
So if we use unsafeCoerce
from a complex object, say a function, to a simple object, say an Int
, what happens is that the pointer to the function gets converted to the simple object. And the value of that simple object is dependent only on where the complex object is located not anything about what it is.
Address Space Layout Randomization
Many software vulnerabilities rely in part on being able to find particular functions in computer memory to access them. For these techniques you create some malicious code somewhere in memory (this can be done a few ways) and then by hijacking the stack pointer convince a program to run your code. In order to combat these exploits many systems implement something called Address Space Layout Randomization or ASLR. With ASLR key components are placed randomly so that their locations cannot be predicted. This means even if you do get control of the stack pointer it is hard to find where your malicious code is placed. ALSR is not the only technique used against these attacks but it is one we care about.
For Linux systems you can check of ASLR is on using
cat /proc/sys/kernel/randomize_va_space
Or since this is code-golf:
sysctl -anr "e_v"
There are three levels of security here 0
for off, and 1
and 2
for varying levels of on. My computer has this set to 2
(and your computer probably does too).
You can also set this. However do not do this unless you know what you are doing. Please do not mess with your kernel (unless you know what you are doing). For this reason I'm not going to include the command to do this, but you can look it up online or figure it out from the commands I already gave.
What this means for us is that the locations for objects allocated by Haskell at runtime is random or at least non-deterministic. This means that when we take a complex object like a function and convert it to a simple object like an Int
the value of that object is random too.
The program
So that's what our program does. We coerce a function, (+)
because it is short. We wrap this in a list and print it using putStr
. Since putStr
expects a string the compiler infers that the output of unsafeCoerce(+)
must be a Char
, which is a simple type.
So we end up printing a random one character string.
A more normal solution, 44 bytes
import System.Random
main=randomIO>>=putChar
For completion sake this is the "normal" Haskell approach. It is slightly shorter but far more boring.
Pyth, 2 bytes
O0
Explanation:
0 0
O Random float in [0, 1)
It's that, when O
has 0
as its argument, it simply returns a random float between 0
and 1
, exclusive.
Let's do it just for the heck of it!
Also, it seems that this can be sorta retro (thanks to 34718/mbomb007):
Pyth, 2 bytes
OT
Explanation:
T 10
O Random integer in [0, 10]
-
5\$\begingroup\$ dilbert.com/strip/1992-09-08 \$\endgroup\$mbomb007– mbomb0072016年12月02日 21:54:31 +00:00Commented Dec 2, 2016 at 21:54
Python 2, 11 bytes
print id(1)
-
1\$\begingroup\$ That's the one I found. I had
id(0)
, though. :) \$\endgroup\$mbomb007– mbomb0072016年11月30日 20:32:10 +00:00Commented Nov 30, 2016 at 20:32 -
2\$\begingroup\$ I beat you, technically, since I'm the OP and found it before I posted the question. I didn't want to post the question and immediately post my shortest answer. \$\endgroup\$mbomb007– mbomb0072016年11月30日 20:33:06 +00:00Commented Nov 30, 2016 at 20:33
Bash (procps-ng), 2 bytes
ps
$$
is also a solution.
-
1\$\begingroup\$ If so,
w
is shorter and also comes from procps. \$\endgroup\$liori– liori2016年12月01日 02:48:01 +00:00Commented Dec 1, 2016 at 2:48 -
1\$\begingroup\$ You edited this into a duplicate of my answer. \$\endgroup\$user62131– user621312016年12月01日 11:30:03 +00:00Commented Dec 1, 2016 at 11:30
-
\$\begingroup\$ @ais523 oops, sorry. \$\endgroup\$Riker– Riker2016年12月01日 14:57:42 +00:00Commented Dec 1, 2016 at 14:57
-
\$\begingroup\$ Nice, my attempt was
gzip
. \$\endgroup\$Makonede– Makonede2021年04月15日 22:07:37 +00:00Commented Apr 15, 2021 at 22:07
Commodore 64 Basic, 4 bytes
1S|0
PETSCII substitution: |
= SHIFT+Y
The zero page of a Commodore 64 is an area of 256 bytes of memory that can be accessed faster than the rest of RAM. Consequently, programs (such as the BASIC interpreter) use it for frequently-accessed data, and the CPU itself stores some of its internal state here. The contents are subject to change without notice.
The BASIC program above, ungolfed, is 1 SYS 0
, ie. transfer execution to memory location 0. This starts executing the zero page as code. Normally, when the BASIC interpreter starts running a program, the first 16 bytes are
2F 37 00 AA B1 91 B3 22
22 00 00 4C 00 00 00 00
so SYS 0
would execute the following
00: ROL-AND 37,ドルA - Undocumented opcode: rotate the value at memory location 0x37 left, and store the result in the accumulator
02: BRK - Call the interrupt vector
The overall result is to output the BASIC READY.
prompt and return control to the user. However, memory location 0x00 is the CPU's I/O direction register, and memory location 0x01 is CPU's I/O address register. If you've done something that changes these before running the program, the results can be unpredictable, ranging from outputting garbage to locking up the computer (the 0x22 usually contained in memory location 0x07, if executed as an instruction, is an undocumented HALT
opcode).
Alternatively, a more reliably unpredictable program is the four-byte
1?TI
Prints the elapsed time, in jiffies (1/60 of a second), since system power-on.
PowerShell, (削除) 4 (削除ここまで) 2 bytes
(crossed out 4 still looks like 4)
ps
This is the alias for Get-Process
which will output the current process listing as a table, including handles, private memory, CPU time, etc.
Execute it via something like the following:
C:\Tools\Scripts\golfing>powershell.exe "ps"
-
2\$\begingroup\$ I have to admit, I checked to see if it was actually crossed out. You really can't even tell. \$\endgroup\$Carcigenicate– Carcigenicate2016年12月01日 00:25:53 +00:00Commented Dec 1, 2016 at 0:25
-
\$\begingroup\$ @Carcigenicate I can tell if it is crossed without selecting the text (Ubuntu 16.04.1, Chrome 54.0.2840.100). \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2016年12月01日 19:38:04 +00:00Commented Dec 1, 2016 at 19:38
-
\$\begingroup\$ Visible on android as well, though not clearly :p \$\endgroup\$tomsmeding– tomsmeding2016年12月01日 23:03:31 +00:00Commented Dec 1, 2016 at 23:03
-
\$\begingroup\$ It looks fine in the iOS app. \$\endgroup\$Mateusz Piotrowski– Mateusz Piotrowski2016年12月06日 12:43:13 +00:00Commented Dec 6, 2016 at 12:43
-
\$\begingroup\$ I don't have the rep to do it myself, but you could do 04 and strike it out. \$\endgroup\$Bobson– Bobson2016年12月06日 14:09:45 +00:00Commented Dec 6, 2016 at 14:09
Notepad, 1 keystroke
F5
Pressing F5 inserts the time and date in the format (h)h:mm XM (m)m/(d)d/yyyy
. For example: 12:00 AM 1/1/1970
-
4\$\begingroup\$ Today I learned... \$\endgroup\$Citty– Citty2021年04月29日 17:44:05 +00:00Commented Apr 29, 2021 at 17:44
-
1\$\begingroup\$ I tried F5 is Notepad++, and it asks to run a program, Win+R style. \$\endgroup\$mbomb007– mbomb0072021年04月29日 19:13:10 +00:00Commented Apr 29, 2021 at 19:13
-
\$\begingroup\$ Oh wow... never knew this \$\endgroup\$Seggan– Seggan2022年08月04日 14:52:56 +00:00Commented Aug 4, 2022 at 14:52
-
\$\begingroup\$ In my laptop I also had to press Fn \$\endgroup\$Rhaixer– Rhaixer2023年06月10日 03:32:57 +00:00Commented Jun 10, 2023 at 3:32
-
\$\begingroup\$ @UndoneStudios You can lock Fn with Fn+Esc first. \$\endgroup\$Makonede– Makonede2023年06月10日 15:42:30 +00:00Commented Jun 10, 2023 at 15:42
Baby Language, 0 bytes
I didn't submit this originally because I thought it postdated the question. I was wrong; the language did have an interpreter created in time. It's also probably the least cheaty 0-byte solution I've seen (given that a 0-byte program is specified to do exactly what the program asks, and not for the purpose of cheating on golfing challenges).
Baby Language is specified to ignore the program it's given and do something at random. (The interpreter linked on the Esolang page generates a random legal BF program and runs it.) That seems like a perfect fit for this challenge.
Zsh, 5 bytes
<<<$$
Prints PID.
C, (削除) 25 (削除ここまで) 21 bytes
Thanks to pseudonym117 for saving 4 bytes.
main(i){putchar(&i);}
Compiled with gcc -o test lol.c
(yeah I'm quite original with my file's name...), and ran with ./test
.
It does what it says: prints the character corresponding to the memory address of i
, which is defined at runtime, so it should be non-deterministic.
-
\$\begingroup\$ 1. Can you skip the
&
as the value of a variable on the stack isn't defined? 2. You have a constant number of things on the stack, so is the memory address ifi
constant? \$\endgroup\$Riley– Riley2016年11月30日 22:14:54 +00:00Commented Nov 30, 2016 at 22:14 -
2\$\begingroup\$
i
becomes what's normally calledargc
, so you're right, it will always be 1 unless there are more arguments. I can't believe I didn't rememeber that. I'm still not sure why the location changes, but if it works it works. \$\endgroup\$Riley– Riley2016年11月30日 23:05:26 +00:00Commented Nov 30, 2016 at 23:05 -
2\$\begingroup\$ It changes on modern operating systems due to ASLR, a security feature designed to make it harder for exploits to guess addresses. You'll get a consistent result on some older OSes. \$\endgroup\$user62131– user621312016年12月01日 01:03:32 +00:00Commented Dec 1, 2016 at 1:03
-
2\$\begingroup\$ You can save a little by replacing
putchar
withreturn
\$\endgroup\$ceilingcat– ceilingcat2016年12月02日 05:15:59 +00:00Commented Dec 2, 2016 at 5:15 -
1\$\begingroup\$ On modern compilers with warnings for not declaring the type of a parameter, you could do:
main(){printf("%d");}
\$\endgroup\$Myria– Myria2016年12月07日 20:23:24 +00:00Commented Dec 7, 2016 at 20:23
BrainfuckX and small s.c.r.i.p.t. (etc) polyglot - 2 bytes
?.
? - Randomize the value in the current cell
. - Send current cell to stdout
05AB1E, 2 bytes
žd
Outputs current microseconds from the executing machine's internal clock.
Or you could do something like this...
05AB1E, 3 bytes
A.r
Outputs a randomly shuffled lower-case alphabet.
Or this also works:
A.R
Outputs a random letter from the alphabet.
Or this also works, and is cooler:
05AB1E, 9 bytes
"ž"A.RJ.V
Outputs randomly one of these:
ž 23 > ža push current hours
žb push current minutes
žc push current seconds
žd push current microseconds
že push current day
žf push current month
žg push current year
žh push [0-9]
ži push [a-zA-Z]
žj push [a-zA-Z0-9_]
žk push [z-aZ-A]
žl push [z-aZ-A9-0_]
žm push [9-0]
žn push [A-Za-z]
žo push [Z-Az-a]
žp push [Z-A]
žq push pi
žr push e
žs pop a, push pi to a digits (max. 100000)
žt pop a, push e to a digits (max. 10000)
žu push ()<>[]{}
žv push 16
žw push 32
žx push 64
žy push 128
žz push 256
Perl, 5 bytes
say$$
Outputs the process ID and a newline.
-
1\$\begingroup\$ Similarly:
say[]
andsay{}
output the reference address of an array or hash. \$\endgroup\$Dom Hastings– Dom Hastings2022年01月12日 09:52:56 +00:00Commented Jan 12, 2022 at 9:52
Python 2, 29 bytes
import os
print os.urandom(9)
Sadly not the first time writing code on a smartphone.
-
1\$\begingroup\$ $. would also work Nice approach though :) \$\endgroup\$Jatin Dhankhar– Jatin Dhankhar2016年12月26日 15:56:23 +00:00Commented Dec 26, 2016 at 15:56
Groovy, 9 bytes
{print{}}
Outputs:
Script1$_run_closure1@2c8ec01c
Because it outputs the memory address of the closure it is non-deterministic.
-
\$\begingroup\$ And I thought Kotlin was the way to the shortest possible JVM solution. \$\endgroup\$F. George– F. George2016年11月30日 23:33:09 +00:00Commented Nov 30, 2016 at 23:33
-
\$\begingroup\$ Trying to put this into a file and running
groovy Script1.groovy
, I get an error:Ambiguous expression could be either a parameterless closure expression or an isolated open code block;
. How do I use this? \$\endgroup\$Paŭlo Ebermann– Paŭlo Ebermann2016年12月04日 01:45:36 +00:00Commented Dec 4, 2016 at 1:45 -
\$\begingroup\$ Assign it to a closure variable, then call it. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2016年12月05日 05:14:26 +00:00Commented Dec 5, 2016 at 5:14
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
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?)
-
-
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– KSmarts2017年10月06日 18:22:49 +00:00Commented 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– mbomb0072017年10月06日 19:08:31 +00:00Commented 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– Mark2017年10月06日 22:07:28 +00:00Commented Oct 6, 2017 at 22:07
-
1\$\begingroup\$ I'm fairly certain this is deterministic. \$\endgroup\$Riker– Riker2016年11月30日 20:33:33 +00:00Commented Nov 30, 2016 at 20:33
-
\$\begingroup\$ @EasterlyIrk in a deleted comment mbomb said this was ok \$\endgroup\$Blue– Blue2016年11月30日 20:34:07 +00:00Commented Nov 30, 2016 at 20:34
-
\$\begingroup\$ Oh, okay. Seems deterministic to me, but OP rules. \$\endgroup\$Riker– Riker2016年11月30日 20:35:03 +00:00Commented Nov 30, 2016 at 20:35
-
\$\begingroup\$ In my edit history I had a 3 byte version with proper randomness \$\endgroup\$Blue– Blue2016年11月30日 20:35:36 +00:00Commented Nov 30, 2016 at 20:35
-
1\$\begingroup\$ @EasterlyIrk If the current time is deterministic, then so are pseudo-random numbers, since that's what they're seeded with. The goal is not "randomness". The goal is non-determinism. \$\endgroup\$mbomb007– mbomb0072016年11月30日 20:35:41 +00:00Commented Nov 30, 2016 at 20:35
Emotinomicon, 4 bytes
😀😅🎲⏬
Explanation:
😀😅🎲⏬
😀 push 0. Stack: [0]
😅 push 1. Stack: [1]
🎲 random[pop;pop]. Stack: [1 or 0]
⏬output
-
\$\begingroup\$ The language's name is Emotinomicon \$\endgroup\$acrolith– acrolith2016年12月02日 18:46:47 +00:00Commented Dec 2, 2016 at 18:46
-
\$\begingroup\$ @daHugLenny How did I missed that :) \$\endgroup\$Linnea Gräf– Linnea Gräf2016年12月02日 19:48:37 +00:00Commented Dec 2, 2016 at 19:48
-
\$\begingroup\$ This is 4 bytes. Emotinomicon uses its own code page, meaning each emoji used is one byte. \$\endgroup\$Makonede– Makonede2021年04月29日 16:57:56 +00:00Commented Apr 29, 2021 at 16:57