Re: mapping numbers to 'random' numbers and back



JdV said:

Hello,

I am looking for a simple algorithm to map small integers (4 digits,
0000-1000) to large random-looking numbers in a certain range (5 digits,
10000-99999). For example (just with some made-up numbers), 1 -> 62521,
2 -> 30162, and I need to be able to map the big numbers back to their
originals (30162 -> 2, etc).

Any tips on a method or algorithm to use for this ?

Given that you need a reversible mapping, I suggest you just pick two or
three reversible operations and do them one after the other. For example:

unsigned long hash(unsigned int n)
{
unsigned long h = n << 4;
h ^= 0x01234567;
return h - 42;
}

unsigned int unhash(unsigned long h)
{
unsigned long n = h + 42;
n ^= 0x01234567;
return n >> 4;
}

I haven't checked whether hash() produces numbers in the range you're after,
but it hardly matters - just fiddle with it till it does, and do the
opposite fiddling in unhash().

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
.



Relevant Pages

  • mapping numbers to random numbers and back
    ... I am looking for a simple algorithm to map small integers (4 digits, ... 0000-1000) to large random-looking numbers in a certain range (5 digits, ...
    (comp.programming)
  • "Algorithmic Randomness, Quantum Physics, and Incompleteness"
    ... all finite sequences are to be found infinitely often and ... different members of the infinite set of random numbers. ... Just using random numbers with initial digits 1 thru 9, ... it generated by a shorter input algorithm than the length ...
    (sci.logic)
  • Re: BigNum -- Floating Point
    ... > It means the memory required for representing a number is just ... write an RSA algorithm, for example. ... interest might be something like pi...in base N...to M digits. ... >> wouldn't the gcd() itself need to be able to handle bigints? ...
    (comp.programming)
  • Re: A dungeon generator
    ... rooms will not fit in the map, so your algorithm will fail. ... each level has 16 possible starting templates. ...
    (rec.games.roguelike.development)
  • Re: one sentence proof of 4 Color Mapping Problem; direct method
    ... for you to determine whether you can color this map with THREE ... So an algorithm for this machine would look ... building the algorithm in P to solve the NP-complete problems, ... Perhaps the equivalency is the idea that Eucl geom is what is ...
    (sci.math)