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; #### use List::Util qw/shuffle/; my $x = 1; my $y = 20; my $looptimes = 10; my @numbers = shuffle($x .. $y); say join ",", @numbers[0 .. ($looptimes - 1)];