Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Random 1-1 mapping

by QM (Parson)
on May 28, 2006 at 18:56 UTC ( [id://552205]=note: print w/replies, xml ) Need Help??


in reply to Random 1-1 mapping

If you want to walk the list in a random order ("without replacement" in the vernacular), then you need something to maintain state, and be aware of the size of your list (both initially and at each call).

Your claims of no spare memory make this difficult. Either you need a scheme that implicitly keeps track of which elements have already been returned, or you need to explicitly keep track of them.

On the explicit front, you can keep a record in a bit vector, with one bit for each element already used. However, to get the last element of a large list, using a PRNG will take a long time.

On the implicit front, you need an iterator thats reasonably random, yet is exactly uniformly distributed regardless of the size of your list. This makes me think of a prime modulo operation:

#!/your/perl/here use strict; use warnings; { # closure for "static" variables my $mult = 2**15-1; my $offset = 76543; my $state = 0; sub get_rand { my $list = shift; # ref $state = ($state*$mult+$offset)%scalar(@{$list}); return $list->[$state]; } sub init_get_rand { my $list = shift; $state = rand(scalar(@{$list})); } } my $list = [0..17]; for (1..2) { init_get_rand($list); for ( 1..@{$list} ) { print get_rand($list), " "; } print "\n"; } __OUTPUT__ 8 9 16 11 12 1 14 15 4 17 0 7 2 3 10 5 6 13 2 3 10 5 6 13 8 9 16 11 12 1 14 15 4 17 0 7
Now, I'll be the first to admit I haven't checked on good values for $mult, $offset, or whether lists of certain sizes will cause you to miss values, etc. But you should be able to look up a good PRNG on your own ;)

-QM
--
Quantum Mechanics: The dreams stuff is made of

Replies are listed 'Best First'.
Re^2: Random 1-1 mapping
by tomazos (Deacon) on May 28, 2006 at 19:06 UTC
    I've possibly been confusing with the use of the word iterate. The list is actually directly accessible. I can access element x cheaply and statelessly - the records are fixed sized and contiguous.

    I just want to walk this list in a well-defined, reproducible, random-order.

    I think multiplying by a prime and then dividing by the maximum has something to do with producing the 1-1 mapping (was it RSA or abstract algebra / group/ring theory - I'm too old for this sh*t).

    -Andrew.

      1) Did this code/idea help you?

      2) You need an offset if zero is involved.

      3) Yes, multiplying by a prime mod some other prime will do what you want. There are probably several other configurations that will do the same, and some will be better than what I've given. There are numerous approaches, rules, tests, and other info that you can dig up. I'd start with Mathworld or Wikipedia.

      -QM
      --
      Quantum Mechanics: The dreams stuff is made of

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://552205]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others surveying the Monastery: (5)
As of 2024-03-28 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found