-15
\$\begingroup\$

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

\$\endgroup\$
6
  • 6
    \$\begingroup\$ 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. \$\endgroup\$
    – grovesNL
    Mar 12, 2014 at 18:24
  • 4
    \$\begingroup\$ -1 for changing the rules after posting, cost me 20 points...Also, use the sandbox next time \$\endgroup\$ Mar 12, 2014 at 18:59
  • 3
    \$\begingroup\$ 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. \$\endgroup\$ Mar 12, 2014 at 22:19
  • 1
    \$\begingroup\$ @jcw accepting an answer early and changing rules after an answer is submitted....dirty pool man \$\endgroup\$ Mar 13, 2014 at 3:51
  • 1
    \$\begingroup\$ Just saw this one now. My answer would have been a statistical md5sum /dev/sda \$\endgroup\$ Mar 14, 2014 at 6:43

13 Answers 13

8
\$\begingroup\$

T-SQL, 13

PRINT NEWID()

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

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

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]
\$\endgroup\$
3
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 12, 2014 at 19:50
  • 1
    \$\begingroup\$ 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 \$\endgroup\$
    – nmclean
    Mar 13, 2014 at 12:50
  • \$\begingroup\$ 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 \$\endgroup\$
    – cat
    Dec 26, 2015 at 18:56
4
\$\begingroup\$

Perl, 67 (but growing slowly) char

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.

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

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{$$++}
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
2
  • \$\begingroup\$ clever, but, "It must return a different value on your computer than it does on my computer" \$\endgroup\$ Mar 12, 2014 at 20:02
  • 4
    \$\begingroup\$ @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. \$\endgroup\$ Mar 12, 2014 at 20:39
2
\$\begingroup\$

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
\$\endgroup\$
2
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

Bash, 18, obvious

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

Python, 56

import uuid
import time
print uuid.getnode(),time.time()
\$\endgroup\$
4
  • 3
    \$\begingroup\$ I removed all network cards from both computers. Now they return the same value. \$\endgroup\$ Mar 12, 2014 at 18:53
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 12, 2014 at 19:13
  • \$\begingroup\$ @JonathanVanMatre: Thanks for letting me know \$\endgroup\$
    – Alt
    Mar 12, 2014 at 19:15
  • 3
    \$\begingroup\$ You may want to use import uuid,time instead to reduce your char count. \$\endgroup\$
    – user12205
    Mar 12, 2014 at 19:46
1
\$\begingroup\$

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.

\$\endgroup\$
1
\$\begingroup\$

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

\$\endgroup\$
2
  • \$\begingroup\$ +1 for careful reading, and for being the apparent shortest true function. Hopefully OP will sort this out. \$\endgroup\$ Mar 13, 2014 at 3:53
  • \$\begingroup\$ 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. \$\endgroup\$ Mar 13, 2014 at 13:55
0
\$\begingroup\$

bash+curl - 24 chars +20

curl -s time.nist.gov:13
\$\endgroup\$
8
  • \$\begingroup\$ Violates the second rule "It must return a different value on your computer than it does on my computer" \$\endgroup\$
    – grovesNL
    Mar 12, 2014 at 18:23
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Mar 12, 2014 at 18:24
  • 2
    \$\begingroup\$ Then he should say "don't use clock calls" \$\endgroup\$ Mar 12, 2014 at 18:29
  • 4
    \$\begingroup\$ @DavidWilkins Easy: Unplug network cable from my computer and your computer. Same results. \$\endgroup\$
    – nmclean
    Mar 12, 2014 at 18:29
  • 3
    \$\begingroup\$ Mandatory xkcd.com/1340 \$\endgroup\$
    – user80551
    Mar 12, 2014 at 18:37
0
\$\begingroup\$

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!

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

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