Write a function that returns something different every time it is called

-14

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

jcw

Posted 2014-03-12T18:17:16.887

Reputation: 195

Question was closed 2014-03-13T10:49:29.417

6It'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. – grovesNL – 2014-03-12T18:24:52.160

3-1 for changing the rules after posting, cost me 20 points...Also, use the sandbox next time – David Wilkins – 2014-03-12T18:59:24.247

@TheDoctor - Okay.... I am happy to change it if someone else comes uo with something shorter - what is the normal etiquette for picking answers? – jcw – 2014-03-12T21:55:08.370

2Give 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. – Jonathan Van Matre – 2014-03-12T22:19:17.420

@jcw accepting an answer early and changing rules after an answer is submitted....dirty pool man – David Wilkins – 2014-03-13T03:51:00.410

1Just saw this one now. My answer would have been a statistical md5sum /dev/sda – Digital Trauma – 2014-03-14T06:43:46.743

Answers

3

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{$$++}

Konrad Borowski

Posted 2014-03-12T18:17:16.887

Reputation: 11 185

8

T-SQL, 13

PRINT NEWID()

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

Jonathan Van Matre

Posted 2014-03-12T18:17:16.887

Reputation: 2 307

4

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.

mob

Posted 2014-03-12T18:17:16.887

Reputation: 2 506

3

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]

Hovercouch

Posted 2014-03-12T18:17:16.887

Reputation: 659

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 – 2015-12-26T18:56:16.020

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. – Thomas Baruchel – 2014-03-12T19:50:41.710

1You 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 – nmclean – 2014-03-13T12:50:30.150

2

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.

Allen Gould

Posted 2014-03-12T18:17:16.887

Reputation: 131

clever, but, "It must return a different value on your computer than it does on my computer" – Jordon Biondo – 2014-03-12T20:02:57.777

4@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. – Allen Gould – 2014-03-12T20:39:40.827

2

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

mniip

Posted 2014-03-12T18:17:16.887

Reputation: 9 396

2

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.

VisioN

Posted 2014-03-12T18:17:16.887

Reputation: 4 490

1

Bash, 18, obvious

head /dev/urandom

user80551

Posted 2014-03-12T18:17:16.887

Reputation: 2 520

4violates the 3rd rule: "Your function must be guaranteed to be different (no random numbers)" – sigil – 2014-03-12T18:47:07.680

Its pseudorandom :) , and that sentence is a bit ambiguous. – user80551 – 2014-03-12T18:49:25.577

1@user80551 that's even worse – John Dvorak – 2014-03-12T18:53:47.800

@JanDvorak At least he tried. – Ismael Miguel – 2014-03-12T19:36:04.303

1

Python, 56

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

Alt

Posted 2014-03-12T18:17:16.887

Reputation: 119

3I removed all network cards from both computers. Now they return the same value. – John Dvorak – 2014-03-12T18:53:29.017

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. – Jonathan Van Matre – 2014-03-12T19:13:45.137

@JonathanVanMatre: Thanks for letting me know – Alt – 2014-03-12T19:15:10.430

3You may want to use import uuid,time instead to reduce your char count. – user12205 – 2014-03-12T19:46:48.613

1

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

Thomas Baruchel

Posted 2014-03-12T18:17:16.887

Reputation: 1 590

+1 for careful reading, and for being the apparent shortest true function. Hopefully OP will sort this out. – Jonathan Van Matre – 2014-03-13T03:53:15.553

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. – Thomas Baruchel – 2014-03-13T13:55:38.290

1

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.

user10766

Posted 2014-03-12T18:17:16.887

Reputation:

0

bash+curl - 24 chars +20

curl -s time.nist.gov:13

David Wilkins

Posted 2014-03-12T18:17:16.887

Reputation: 593

Violates the second rule "It must return a different value on your computer than it does on my computer" – grovesNL – 2014-03-12T18:23:05.973

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 – David Wilkins – 2014-03-12T18:24:26.023

It's about the principle. If that were the case then you might as well just query the system clock directly. – grovesNL – 2014-03-12T18:27:29.870

1Then he should say "don't use clock calls" – David Wilkins – 2014-03-12T18:29:01.133

4@DavidWilkins Easy: Unplug network cable from my computer and your computer. Same results. – nmclean – 2014-03-12T18:29:31.820

@nmclean Who here would ever have a computer incapable of getting to stackexchange (tongue in cheek) – David Wilkins – 2014-03-12T18:31:12.593

@DavidWilkins: Sure. The question has some issues regardless. – grovesNL – 2014-03-12T18:31:45.503

3

Mandatory https://xkcd.com/1340/

– user80551 – 2014-03-12T18:37:17.070

0

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!

Alt

Posted 2014-03-12T18:17:16.887

Reputation: 119

Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'f' is not defined – cat – 2015-12-26T18:51:27.177

With which version is this meant to work? It errors the same way with python2 and py3 – cat – 2015-12-26T18:52:00.467