No such thing as a small change PerlMonks

### Re^2: Random 1-1 mapping

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

in reply to Re: Random 1-1 mapping

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

Replies are listed 'Best First'.
Re^3: Random 1-1 mapping
by blokhead (Monsignor) on May 28, 2006 at 21:26 UTC
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 { \$_[1] ? gcd(\$_[1], \$_[0] % \$_[1]) : \$_[0] }

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://552204]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2023-11-29 11:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?

No recent polls found

Notices?