0

I m looking for a function that permit me to generate a different 4 digit number (>0) each time (to 4^10 possibilities) corresponding to a parameter that will never be the same (like current timestamp)

Can you help me ?

EDIT : Explanations :

  • I need to generate a 4 digit number from 0 to 9, no negative numbers
  • It has to be different each time
  • I dont accept collisions except if all the possibilities have already been generated
  • I need the generated number to be drastically different from the previous one (1234 - 5423 is accepted, but 1234 - 1235 is not)

  • A generation is applied whenever a user asks it (it's included in a software program), there's no time fixed between two calls to the function

mfrachet
  • 101
  • 3
  • I think you mean 10^4 possibilities? And are you including numbers with leading zeros? When you say "different" are you implying that there can be no collisions in the course of this evaluation? The same number can never appear twice? How many times will the function be called? Is there a reason you cannot generate a list of numbers, shuffle them, and then draw them one at a time as needed? – Mr.Wizard Feb 06 '15 at 14:58
  • @David If collisions are acceptable that would work, but then why bother with a parameter at all? Just call RandomInteger[{1000, 9999}] each time and be done with it. – Mr.Wizard Feb 06 '15 at 15:00
  • Related, or possible duplicate: (7713) – Mr.Wizard Feb 06 '15 at 15:01
  • I editted, collisions are not allowed. 0 are included. So the same number cant appear twice but could start with a 0. I need to generate the different number at a different time in fact, so display it now is not the good solution. – mfrachet Feb 06 '15 at 15:02
  • 3
    question doesn't say random.. just start at zero and increment the val by one each call. You really need to provide some better description of exactly what you are trying to do. – george2079 Feb 06 '15 at 15:04
  • Question edited – mfrachet Feb 06 '15 at 15:11
  • 3
    "drastically different" is not a well defined equivalence relationship. – Dr. belisarius Feb 06 '15 at 15:17
  • I have put this on hold until additional details are provided. – Mr.Wizard Feb 06 '15 at 15:28
  • Please reference this Stack Overflow Q&A: (196017). – Mr.Wizard Feb 06 '15 at 15:32
  • Just cycle through the list RandomSample@Range[1000, 9999]. – Szabolcs Feb 06 '15 at 15:47
  • 2
    @belisarius "drastically different" is actually an inequivalence relationship.. Okay, I'm not being entirely facetious here, for inequivalence one need not have transitivity. (On a distantly related note, I recently watched the miniseries ""Fiendens fiende", subtitled. Hard to believe the same person portrayed both Carl Hamilton and Martin Beck.) – Daniel Lichtblau Feb 06 '15 at 16:52
  • @DanielLichtblau Somehow I knew that my comment was doomed to be answered by you :) – Dr. belisarius Feb 06 '15 at 17:07

1 Answers1

3

There may be a better way to accomplish this, but I think this illustrates what you are asking.

 i = 1;
 Clear[uq];
 sequence = RandomSample[Range[10^4], 10^4] ;
 uq[param_] := uq[param] = sequence[[i++]] 



 uq["cat"]
 uq["dog"]
 uq["horse"]
 uq["cat"]
561
4835
2597
561

.. supposing we need to recover the values,

 invert[n_] := (Select[ DownValues[uq]  , #[[2]] == n &  ][[1, 1]] /. 
      HoldPattern[uq[x_]] :> x )[[1]]
 invert[561]

"cat"

george2079
  • 38,913
  • 1
  • 43
  • 110