use Data::Dumper; my @l = (2, 3, 4, 5, 2, 7, 3, 8 ,9, 1, 3 ,5 ,7, 8, 9, 1, 2, 3, 4, 5, 2, 7, 3, 8 ,9, 1, 3 ,5 ,7, 8, 9, 1); my @m = map { $_ % 4 == 0 ? ($l[$_-1], int(rand(9999))) : $l[$_-1] } 1..$#l+1; print Dumper \@m;