#!/usr/bin/perl use strict; my %chosen; # you don't need a 10-element array to draw from while ( keys %chosen < 3 ) { my $pick = int( rand(10)) + 1; $chosen{$pick} = undef; } print join " ", keys %chosen, "\n"; # (you could "sort {$a<=>$b) keys %chosen", if that matters)