in reply to random index in array - no duplicates
two problemsmy @tab_anonyme; for $i (0..$#my_data) { $tab_anonyme[$i][0] = $my_data[$i][0]; # data to keep $tab_anonyme[$i][1] = int ( rand (100) ) + 1; # anonyme label } # verification my $flag_duplicate; do { $flag_duplicate = "false"; for $i (0..$#tab_anonyme) { for $i2 (0..$#tab_anonyme) { if ($i2 == $i) { next; } # no testing of a value against i +tself if ( $tab_anonyme[$i][1] == $tab_anonyme[$i2][1] ) { $tab_anonyme[$i2][1] = int ( rand (100) ) + 1; $flag_duplicate = "true"; } } } } while ( $flag_doublon eq "true" );
- in rand(X), if X is smaller than the number of lines of the array @mydata => infinite loop, make it four or five time bigger
- time taken is not very predictible
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: random index in array - no duplicates
by Anonymous Monk on Jul 08, 2016 at 13:41 UTC |