Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Re^2: random pairs

by Perlbotics (Canon)
on Jul 13, 2012 at 20:26 UTC ( #981716=note: print w/replies, xml ) Need Help??

in reply to Re: random pairs
in thread random pairs

Unfortunately, that algorithm does not avoid unwanted pairs like [1,23] and [23,1].

Here is a naive try until it works approach:

use strict; use warnings; use List::Util qw(shuffle); my (%keys, @rnds, @pairs); do { %keys = (); @rnds = shuffle (0..99); @pairs = map{ my $r = $rnds[$_]; $keys{"$_,$r"}++; $keys{"$r,$_"}++; [ $_, $r ] } (0..99); } while ( scalar keys %keys != 200 ); # no symmetry means 200 distinct + pairs # @pairs = shuffle( @pairs ); # uncomment if wanted printf( "%3d %3d\n", @{$pairs[$_]}) for (0..99);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://981716]
[Discipulus]: no fear: moonday i'll back (unless a big quake ... ;=)
[Corion]: Discipulus: Being out in nature is great, at least as long as you have a dry place to sleep and a warm fire ;)

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (12)
As of 2017-02-24 09:18 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (353 votes). Check out past polls.