Re: mapping numbers to 'random' numbers and back
- From: Richard Heathfield <invalid@xxxxxxxxxxxxxxx>
- Date: Fri, 11 Aug 2006 15:17:01 +0000
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)
.
- References:
- Prev by Date: Re: random numbers
- Next by Date: Key establishment question
- Previous by thread: mapping numbers to 'random' numbers and back
- Next by thread: Re: mapping numbers to 'random' numbers and back
- Index(es):
Relevant Pages
|