# Shortest code to produce non-deterministic output

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.

• @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 Nov 30 '16 at 20:44
• @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 Nov 30 '16 at 21:00
• @MartinEnder Yeah, I agree with Nathan. – mbomb007 Nov 30 '16 at 21:01
• 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. – user62131 Nov 30 '16 at 21:23
• 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. – WGroleau Dec 1 '16 at 3:30

# 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]
• One of the cooler answers here, this should win over a "Current Date" solution in my opinion. – Magic Octopus Urn Nov 30 '16 at 21:39
• that above it seems the disassebly, step by step of the begin of some function in 386 intel cpu assembly – RosLuP Dec 2 '16 at 22:05

# Java 7, 3330 27 bytes

int a(){return hashCode();}

Because Java.

• Because Java. Probably the best explanation of Java ever. – F. George Nov 30 '16 at 21:35
• @carusocomputing I had toString in a previous version but then the return type is String which is longer than int. Save the bytes! :] – Poke Nov 30 '16 at 22:17
• Is this valid as a function? hashCode() is an abbreviation here for this.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. – user62131 Dec 1 '16 at 1:08
• In Java 8: ()->hashCode() for 14 bytes. Just sayin' ;) – Olivier Grégoire Dec 1 '16 at 16:58
• @pts I'm going based off of the meta post discussing the default submit structure for posts. Functions are allowed by default unless the challenge specifies that a full program is required. – Poke Dec 2 '16 at 18:36

# 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.

• MATLAB has a builtin for this? Why? – ETHproductions Dec 3 '16 at 20:46
• @ETHproductions The programmer suggested it – Eddie Curtis Dec 5 '16 at 16:04

# R, 1 byte

t

Outputs the function's source code and a memory pointer address which changes with every (re-)start of R.

# 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.

• Of all the things I thought people would use on my GitHub, I didn't think a joke language interpreter would be one :P – Kade Dec 2 '16 at 18:41
• @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. – mbomb007 Dec 2 '16 at 19:36
• Though it'd be non-competing unless I looked at the original .NET interpreter, I guess. – mbomb007 Dec 2 '16 at 19:43

## Labyrinth, 5 bytes

v
!
@

Either prints 0 or nothing (50% chance each).

Try it online!

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 @.

• This isn't guaranteed to terminate, but I was going to post this exactly so +1 – Daniel Dec 1 '16 at 3:59
• 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 – MildlyMilquetoast Dec 1 '16 at 4:19
• It was . in the program I was using for testing, but somehow became a , when I copied it onto PPCG. Fixed. – user62131 Dec 1 '16 at 5:04
• @Dopapp it terminates almost surely though, with probability 1. I'd take that as a guarantee ;) – Oliphaunt - reinstate Monica Dec 1 '16 at 9:17
• @JamesHolderness I think you can do it in 1. Not sure if this counts though... – MildlyMilquetoast Dec 6 '16 at 18:18

# 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.

• So / is not included in the byte count? – Esolanging Fruit Dec 1 '16 at 5:36
• @Challenger5 The slash is optional in command blocks, not allowed in the server console, and mandatory in the chat interface. – Pavel Dec 1 '16 at 5:47
• Actually, it should be 4 bytes + 2 blocks (Command block and redstone source), or 6 blytes – RudolfJelin Dec 2 '16 at 16:38
• @RudolphJelinek You can run it from the console without command blocks. – Pavel Dec 2 '16 at 17:10
• Another non-deterministic option would be help in a command block, which is 4 bytes (and doesn't require pirated games). – Pokechu22 Dec 6 '16 at 19:03

# 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.

• Even the error messages are verbose! – Destructible Lemon Dec 1 '16 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>

• Do you not need it to be Date() to invoke the function? – milk Nov 30 '16 at 21:00
• @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, so Date is a valid entry. – ETHproductions Nov 30 '16 at 21:06
• That makes sense. – milk Nov 30 '16 at 21:17
• Typing Date to the JavaScript console of my browser produces deterministic output, it always produces this: function Date() { [native code] }. You probably mean Date(), 6 bytes. – pts Dec 2 '16 at 17:51
• if "date" above is ok than "malloc(8)" or "time(0)" would be ok in C too – RosLuP Dec 2 '16 at 21:45

ps

Prints PID.

# 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

## Pyke, 1 byte

C

Try it here!

Outputs the current time

• I'm fairly certain this is deterministic. – Rɪᴋᴇʀ Nov 30 '16 at 20:33
• @EasterlyIrk in a deleted comment mbomb said this was ok – Blue Nov 30 '16 at 20:34
• Oh, okay. Seems deterministic to me, but OP rules. – Rɪᴋᴇʀ Nov 30 '16 at 20:35
• In my edit history I had a 3 byte version with proper randomness – Blue Nov 30 '16 at 20:35
• @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. – mbomb007 Nov 30 '16 at 20:35

# 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! • why "main(n){puts(&n+1);}" and not "main(n){puts(&n);}"? – RosLuP Dec 2 '16 at 22:23 • @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. – cat Dec 2 '16 at 22:30 • 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 – RosLuP Dec 2 '16 at 22:41 • 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. – Guntram Blohm supports Monica Dec 5 '16 at 15:11 • @GuntramBlohm You're right and that's very interesting, although for me pointers are 8 bytes and ints are 4 bytes. – cat Dec 5 '16 at 15:29 # PHP, 12 bytes <?=uniqid(); Outputs a unique ID 583f4da627ee3 based on the current time in microseconds. • <?=time(); <-- 10 bytes. – Ismael Miguel Nov 30 '16 at 23:23 • @IsmaelMiguel uniqid() is 1'000'000 times more undetermined than time() ;) – Mario Nov 30 '16 at 23:40 • I'm not saying the oposite. But proposing another answer. You're free to pick that one. – Ismael Miguel Nov 30 '16 at 23:45 • @IsmaelMiguel someone else gave the same answer already... – Mario Nov 30 '16 at 23:46 # Groovy, 9 bytes {print{}} Outputs: Script1$_run_closure1@2c8ec01c

Because it outputs the memory address of the closure it is non-deterministic.

• And I thought Kotlin was the way to the shortest possible JVM solution. – F. George Nov 30 '16 at 23:33
• 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? – Paŭlo Ebermann Dec 4 '16 at 1:45
• Assign it to a closure variable, then call it. – Magic Octopus Urn Dec 5 '16 at 5:14

# Emotinomicon, 15 bytes

😀😅🎲⏬

Explanation:

😀😅🎲⏬
😀      push 0. Stack: [0]
😅    push 1. Stack: [1]
🎲  random[pop;pop]. Stack: [1 or 0]
⏬output
• The language's name is Emotinomicon – acrolith Dec 2 '16 at 18:46
• @daHugLenny How did I missed that :) – Roman Gräf Dec 2 '16 at 19:48

# Borland C on Windows, 12 bytes

m(){puts();}

I rewrote it because they say is possible to use one function. Compiler has not check the argument so compile it; but puts see one address 'nobody' know and begin to print what point that address until find the byte 0x00. It could be not ok if that address is out of memory reserved to program but here print something

• This doesn't give nondeterministic output, it just segfaults every time. – cat Dec 1 '16 at 14:26
• Alternatively, if you get something other than a segfault, what compiler ?? – cat Dec 1 '16 at 14:35
• @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 – RosLuP Dec 1 '16 at 14:53
• @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. – Peter Cordes Dec 6 '16 at 14:42
• @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). – Peter Cordes Dec 6 '16 at 14:47

# 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.