Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: random pairs

by monsoon (Pilgrim)
on Jul 13, 2012 at 21:45 UTC ( #981732=note: print w/ replies, xml ) Need Help??


in reply to random pairs

Without retries

use strict; use warnings; my %pairs; my @left = (0,1,2,5,7,8,10); #put your own predetermined numbers foreach my $val1 (@left){ my @right = (); foreach my $val2 (0..99){ push(@right, $val2) if $val1 != $val2 && (!exists $pairs{$val2} || + $pairs{$val2} != $val1); } $pairs{$val1} = $right[int(rand($#right+1))]; } foreach(@left){ print $_, ',', $pairs{$_}, ' '; }
Update: modified condition so that it works correctly when there's a zero in the predetermined numbers. Also clean under 'use warnings;' now.


Comment on Re: random pairs
Download Code
Re^2: random pairs
by CountZero (Bishop) on Jul 14, 2012 at 12:01 UTC
    This will breakdown when your list of predetermined numbers contains duplicates.

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

    My blog: Imperial Deltronics

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-09-22 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls