 Syntactic Confectionery Delight PerlMonks

### Re: Random 1-1 mapping

by BerntB (Deacon)
 on May 28, 2006 at 18:44 UTC ( #552202=note: print w/replies, xml ) Need Help??

in reply to Random 1-1 mapping

What you want is a perfect hash function, that is inexpensive in computing time?

Why not ask for something simple, like a perpetum mobile? :-)

The \$seed \$input is the place in the original list? Maybe permutation encryption algorithms? But to keep them below \$max...?

Update: If you could keep \$max to a power of 2, you could maybe XOR with a constant? (Update2: The \$seed (constant) would also have to be as many bits as \$max.)

Code example for Update (n.b. \$max must be 2^X):

```my \$max  = 8;
my \$list = [10, 20, 30, 40, 50, 60, 70, 80];

for(my \$seed = 0; \$seed < \$max; \$seed++) {
print "With seed \$seed: ";
for(my \$i = 0; \$i < @\$list; \$i++) {
my \$place = get_rand(\$i, \$seed);
print "\$place:", \$list->[\$place], "  ";
}
print "\n";
}

sub get_rand {
my(\$item, \$seed) = @_;

return \$item ^ \$seed;
}

__END__

# -------------------------------------------
# output:
With seed 0: 0:10  1:20  2:30  3:40  4:50  5:60  6:70  7:80
With seed 1: 1:20  0:10  3:40  2:30  5:60  4:50  7:80  6:70
With seed 2: 2:30  3:40  0:10  1:20  6:70  7:80  4:50  5:60
With seed 3: 3:40  2:30  1:20  0:10  7:80  6:70  5:60  4:50
With seed 4: 4:50  5:60  6:70  7:80  0:10  1:20  2:30  3:40
With seed 5: 5:60  4:50  7:80  6:70  1:20  0:10  3:40  2:30
With seed 6: 6:70  7:80  4:50  5:60  2:30  3:40  0:10  1:20
With seed 7: 7:80  6:70  5:60  4:50  3:40  2:30  1:20  0:10

Replies are listed 'Best First'.
Re^2: Random 1-1 mapping
by tomazos (Deacon) on May 28, 2006 at 18:54 UTC
Update: Your solution is cool but \$max is not guaranteed to be a power of 2. In fact, it almost certainly isn't.

I just scribbled this on a piece of paper, I think I heard something about it to do with the RSA encryption algorithm once...

```\$result = (\$prime_number * \$input + \$seed) % \$max;

Example for \$max = 10, \$prime_number = 7 and \$seed = 5..

```shuffle(5,10,0) == (7 * 0 + 5) % 10 == 5 % 10 == 5
shuffle(5,10,1) == (7 * 1 + 5) % 10 == 12 % 10 == 2
shuffle(5,10,2) == (7 * 2 + 5) % 10 == 19 % 10 == 9
shuffle(5,10,3) == (7 * 3 + 5) % 10 == 26 % 10 == 6
shuffle(5,10,4) == (7 * 4 + 5) % 10 == 33 % 10 == 3
shuffle(5,10,5) == (7 * 5 + 5) % 10 == 40 % 10 == 0
shuffle(5,10,6) == (7 * 6 + 5) % 10 == 47 % 10 == 7
shuffle(5,10,7) == (7 * 7 + 5) % 10 == 54 % 10 == 4
shuffle(5,10,8) == (7 * 8 + 5) % 10 == 61 % 10 == 1
shuffle(5,10,9) == (7 * 9 + 5) % 10 == 68 % 10 == 8

So that works for 7 and 10. Does it work for any \$max and any \$prime_number? If not what conditions will it work for? I am no good with proofs.

-Andrew.

Andrew Tomazos  |  andrew@tomazos.com  |  www.tomazos.com
A mapping of the form
```f(x) = (a*x + b) % m
is 1-to-1 (restricted to 0 <= x < m) as long as gcd(a,m)==1, since that's the only time you can find an inverse to a mod m, and invert the function.

I would suggest splitting your "seed" value into the a & b coefficients in the following way:

• a = largest number less than seed satisfying gcd(a,m)=1
• b = seed - a
```sub gcd { \$_ ? gcd(\$_, \$_ % \$_) : \$_ }

sub shuffle {
my (\$seed, \$max, \$i) = @_;

my \$ca = \$seed;
\$ca-- until gcd(\$ca, \$max) == 1;
my \$cb = \$seed - \$ca;

(\$ca * \$i + \$cb) % \$max;
}

for my \$seed (1 .. 10) {
my @result = map shuffle(\$seed, 15, \$_), 0 .. 14;
print "seed=\$seed ==> @result\n";
}
But take this approach for what it's worth -- The only kinds of mapping you'll get by this process are simple linear (affine) mappings, which may not "look random enough":
```seed=1 ==> 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
seed=2 ==> 0 2 4 6 8 10 12 14 1 3 5 7 9 11 13
seed=3 ==> 1 3 5 7 9 11 13 0 2 4 6 8 10 12 14
seed=4 ==> 0 4 8 12 1 5 9 13 2 6 10 14 3 7 11
seed=5 ==> 1 5 9 13 2 6 10 14 3 7 11 0 4 8 12
seed=6 ==> 2 6 10 14 3 7 11 0 4 8 12 1 5 9 13
seed=7 ==> 0 7 14 6 13 5 12 4 11 3 10 2 9 1 8
seed=8 ==> 0 8 1 9 2 10 3 11 4 12 5 13 6 14 7
seed=9 ==> 1 9 2 10 3 11 4 12 5 13 6 14 7 0 8
seed=10 ==> 2 10 3 11 4 12 5 13 6 14 7 0 8 1 9
For more "unpredictable" orders, you could have more tools at your disposal if \$max is always a prime. Then you take one of the linear sequences above and use it as a sequence of powers of a generator element for the field mod \$max.

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://552202]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2023-09-26 08:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?