0
$\begingroup$

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

$\endgroup$
12
  • $\begingroup$ 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? $\endgroup$
    – Mr.Wizard
    Feb 6, 2015 at 14:58
  • $\begingroup$ @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. $\endgroup$
    – Mr.Wizard
    Feb 6, 2015 at 15:00
  • 3
    $\begingroup$ 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. $\endgroup$
    – george2079
    Feb 6, 2015 at 15:04
  • 3
    $\begingroup$ "drastically different" is not a well defined equivalence relationship. $\endgroup$ Feb 6, 2015 at 15:17
  • 2
    $\begingroup$ @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.) $\endgroup$ Feb 6, 2015 at 16:52

1 Answer 1

3
$\begingroup$

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"

$\endgroup$
3
  • 1
    $\begingroup$ You can simplify this with uq[param_] := uq[param] = sequence[[i++]] I believe. $\endgroup$
    – Mr.Wizard
    Feb 6, 2015 at 15:30
  • $\begingroup$ yes that works, updated. $\endgroup$
    – george2079
    Feb 6, 2015 at 15:35
  • 1
    $\begingroup$ Try RandomSample[Range[10^4]-1]. This makes the numbers go from 0 to 9999, not 1 to 10000 (five digits), and you don't have to (re)write the range twice in case it changes. $\endgroup$ Feb 6, 2015 at 18:43

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