# Write a function that returns something different every time it is called [closed]

All these functional languages that return the same value every time a function is called give me a headache.

Your task is to write a function that returns something different every time it is called.

Here are the rules:

• Your function must continue to return something different every time it is run

• It must return a different value on your computer than it does on my computer

• Your function must be guaranteed to be different (no random numbers)

• +20 if your code gets time from the system or an online clock

• It's impossible "to be different no matter how many times I run it" unless the value returned is infinitely large. You want want to rephrase that. Mar 12, 2014 at 18:24
• -1 for changing the rules after posting, cost me 20 points...Also, use the sandbox next time Mar 12, 2014 at 18:59
• Give people 24 hours at bare minimum. Ideally a contest should be measured in multiple days, not hours. Remember the site has an international audience, so some people may have been asleep for the entire life of the question if you end it after a few hours. As the current accepted answer, I myself think it was too soon for me to win. And there are already shorter answers. Mar 12, 2014 at 22:19
• @jcw accepting an answer early and changing rules after an answer is submitted....dirty pool man Mar 13, 2014 at 3:51
• Just saw this one now. My answer would have been a statistical md5sum /dev/sda Mar 14, 2014 at 6:43

## T-SQL, 13

PRINT NEWID()


Relatively uninspiring challenge, but it's rare that you can show off an instance of true brevity in SQL.

## Python, 28

def f(x=[f]):x+=[1];return x


Since python has mutable default arguments x will be different every time you call f. The starting element gives you a memory address, which changes each time you define the function. Example output:

>>> def f(x=[]): x+=[1]; return x
...
>>> f()
[<function f at 0x1047527d0>, 1]
>>> f()
[<function f at 0x1047527d0>, 1, 1]
>>> f()
[<function f at 0x1047527d0>, 1, 1, 1]

• I really like your solution; you really follow the idea of playing with a function (and with functional ways of programming) as it is required. Mar 12, 2014 at 19:50
• You can't access f in the default argument assignment because it's evaluated before f is assigned. But this would work (and save you 1 character): def f(x=[]):x+=[f];return x Mar 13, 2014 at 12:50
• This returns Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'f' is not defined  because f must be evaluated for the function to reside in memory
– cat
Dec 26, 2015 at 18:56

open X,"+<$0";seek X,66,0;print$y=<DATA>,%ENV;print X++$y __END__ 0  Output the environment of the current machine, plus a hard coded number that is updated in the source code each time you run the script. # Perl (7 bytes) sub{$$}  Well, until processes IDs run out, this should return something else each time it's being called. Alternatively, if you think this is not fine, because it will return the same value for the same instance of program, there is an alternate version which only works in newer versions of Perl which automatically increases process ID every time it's being called. It's 9 bytes long, however. sub{$$++}  DOS - 27 chars echo "X" >> random.txt dir  Not only will your byte size be different, even if you put it in an empty directory your volume serial number won't match mine. • clever, but, "It must return a different value on your computer than it does on my computer" Mar 12, 2014 at 20:02 • @JordonBiondo It does, unless your hard drive happens to have the same volume serial number as mine, and you ran it from the same directory with the same files inside. Mar 12, 2014 at 20:39 # Haskell - 95 import Data.Time.Clock.POSIX import System.IO.Unsafe f _=fromEnum$unsafePerformIO getPOSIXTime


Example:

> f 0
-7269056719496922816
> f 0
-7269056004375922816
> f 0
-7269055316663922816


# C [46 bytes]

#include<stdio.h>
main(int*x){printf("%x",&x);}


Theoretically this may return the same address but practically the probability is rather low.

# Bash, 18, obvious

head /dev/urandom

• violates the 3rd rule: "Your function must be guaranteed to be different (no random numbers)" Mar 12, 2014 at 18:47
• Its pseudorandom :) , and that sentence is a bit ambiguous. Mar 12, 2014 at 18:49
• @user80551 that's even worse Mar 12, 2014 at 18:53
• @JanDvorak At least he tried. Mar 12, 2014 at 19:36

## Python, 56

import uuid
import time
print uuid.getnode(),time.time()

• I removed all network cards from both computers. Now they return the same value. Mar 12, 2014 at 18:53
• Welcome to PPCG! For code golf challenges, our convention is to provide a header (use ## before the header text) with the language name and character count of the answer. Mar 12, 2014 at 19:13
• @JonathanVanMatre: Thanks for letting me know
– Alt
Mar 12, 2014 at 19:15
• You may want to use import uuid,time instead to reduce your char count. Mar 12, 2014 at 19:46

# C++, 38 chars

int *r(){int*s;int n=0;s=&n;return s;}


Run int main(){std::cout<<r();} with the above, and it is guaranteed to return a different address every time.

# J, 11 bytes - True function

In J, printing the date is merely: 6!:0 '' but the question asks for a function (not absolutely sure other solutions follow this requirement), which gives the idea of the following 11-characters function: (6!:0@])&'' which outputs each time something different whatever its argument is (note that function with no-argument should be accepted, but answers with statements for printing the time shouldn't).

• +1 for careful reading, and for being the apparent shortest true function. Hopefully OP will sort this out. Mar 13, 2014 at 3:53
• Thank you for your answer and for having changing the title. Actually, such a function is a little trickier in J than in other languages, because J precisely is a functional language. The trick is: bind the constant argument needed to output the time as a right argument of a function discarding its left argument and returning time from its right argument (hope I am clear enough). The effect of the binding is that the usual right argument will become the left argument of the new function (called "verb" in J) and it will be discarded. Mar 13, 2014 at 13:55

# bash+curl - 24 chars +20

curl -s time.nist.gov:13

• Violates the second rule "It must return a different value on your computer than it does on my computer" Mar 12, 2014 at 18:23
• I challenge you to prove that it will return the same thing on any two systems ever. The simple act of my making the call at one instant disables anyone anywhere from making the same call at the same instant Mar 12, 2014 at 18:24
• Then he should say "don't use clock calls" Mar 12, 2014 at 18:29
• @DavidWilkins Easy: Unplug network cable from my computer and your computer. Same results. Mar 12, 2014 at 18:29
• Mandatory xkcd.com/1340 Mar 12, 2014 at 18:37

## Python 52

A different version of what @Hovercouch suggested:

def f(x=[f]):x+=[__import__("time").time()];return x

>>> f()
[<function f at 0x049F1A30>, 1394652231.0]
>>> f()
[<function f at 0x049F1A30>, 1394652231.0, 1394652232.746]
>>> f()
[<function f at 0x049F1A30>, 1394652231.0, 1394652232.746, 1394652423.971]
>>> f()
[<function f at 0x049F1A30>, 1394652231.0, 1394652232.746, 1394652423.971, 1394652428.137]


Although the odds of getting different output on all computers is not zero!

• Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'f' is not defined
– cat
Dec 26, 2015 at 18:51
• With which version is this meant to work? It errors the same way with python2 and py3
– cat
Dec 26, 2015 at 18:52