my $x = 1; my $y = 20; my $looptimes = 10; my %numbers = (); foreach(1 .. $looptimes) { my $candidate = -1; do { $candidate = int(rand ($y - $x + 1)) + $x; } while(exists $numbers{$candidate}); $numbers{$candidate} = 1; } say join ",", keys %numbers;