I have a matrix that I want to randomize a couple of thousand times, while keeping the row and column totals the same:

```
1 2 3
A 0 0 1
B 1 1 0
C 1 0 0
```

An example of a valid random matrix would be:

```
1 2 3
A 1 0 0
B 1 1 0
C 0 0 1
```

My actual matrix is a lot bigger (about 600x600 items), so I really need an approach that is computationally efficient.

My initial (inefficient) approach consisted of shuffling arrays using the Perl Cookbook shuffle

I pasted my current code below. I've got extra code in place to start with a new shuffled list of numbers, if no solution is found in the while loop. The algorithm works fine for a small matrix, but as soon as I start scaling up it takes forever to find a random matrix that fits the requirements.

Is there a more efficient way to accomplish what I'm searching for? Thanks a lot!

```
#!/usr/bin/perl -w
use strict;
my %matrix = ( 'A' => {'3' => 1 },
'B' => {'1' => 1,
'2' => 1 },
'C' => {'1' => 1 }
);
my @letters = ();
my @numbers = ();
foreach my $letter (keys %matrix){
foreach my $number (keys %{$matrix{$letter}}){
push (@letters, $letter);
push (@numbers, $number);
}
}
my %random_matrix = ();
&shuffle(\@numbers);
foreach my $letter (@letters){
while (exists($random_matrix{$letter}{$numbers[0]})){
&shuffle (\@numbers);
}
my $chosen_number = shift (@numbers);
$random_matrix{$letter}{$chosen_number} = 1;
}
sub shuffle {
my $array = shift;
my $i = scalar(@$array);
my $j;
foreach my $item (@$array )
{
--$i;
$j = int rand ($i+1);
next if $i == $j;
@$array [$i,$j] = @$array[$j,$i];
}
return @$array;
}
```