Instead of using if in your loop, you can use a do {...} while loop, eg:
my @array; for ( 1 .. 10 ) { my $random_number; do { $random_number = rand 600; } while $array[$random_number] eq 'X'; $array[$random_number] = 'X'; }
Here we generate a random number inside the loop and check whether the element at that position has been assigned 'X'. If it is, we try again. Otherwise, we assign 'X' to the selected element.
In reply to Re: Difficulty in randomization
by kejohm
in thread Difficulty in randomization
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |