http://www.perlmonks.org?node_id=957812

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks!
I have the following task:
I have an empty array of length say 600 and a number, say 10.
I want to create 1000 such arrays (and subsequently strings using 'join') by randomly assigning 10 positions in each array to be 'X' while the remaining 590 will be '-'.
I did the following:
@array=(); for($i=1; $i<=10; $i++) { $random_number = rand(600); if($array[$random_number] ne 'X' ) {$array[$random_number]='X';} else {$random_number = rand(600);} } for ($j=0; $j<600; $j++) { if($array[$j] ne 'X') {$array[$j]='-'} } $joined_new_string=join('', @array);

My problem is that I must be doing something wrong, because not all 600 strings produced have 10 positions with 'X' each. And I am suspecting that in my first for loop, I must somehow "remove" the position that is assigned a 'X' in each randomization, because if, for example, in the first run, I assign the 'X' in position 55, the next rand(600) might also give 55 and I will miss one assignment.
How can this be solved?