#!/usr/bin/env perl use strict; use warnings; use List::Util 'shuffle'; use feature 'say'; my $connectionStatus = "down"; my @listOfServers = ( "abc", "def", "lmn", "stu", "xyz" ); while (@listOfServers) { ##THIS: @listOfServers = shuffle @listOfServers; my $randomServer = shift @listOfServers; ##OR THIS, BUT NOT BOTH: my $randomServer = splice @listOfServers, rand @listOfServers, 1; my $connect = 0; say $randomServer; # replace this with your connection test if ( $connect == 1 ) { $connectionStatus = "up"; last; } }