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:

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.

blokhead


In reply to Re^3: Random 1-1 mapping by blokhead
in thread Random 1-1 mapping by tomazos

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.