#!/usr/bin/perl use strict; use warnings 'all'; my @array = qw /first middle last/; my $count = 0; my $sec = 100; $SIG {ALRM} = sub { die "Ran for $sec seconds, trying $count times and failed.\n" }; alarm $sec; my $randomIndex; do { $count ++; $randomIndex = rand $#array; } until $array [$randomIndex] eq "last"; print "Success after $count tries.\n"; __END__ Ran for 100 seconds, trying 29495241 times and failed. #### Success after 2 tries.