0

I am trying to use the rand function to generate numbers from an array containing 3 ranges. This is unfortunately not working. I know I am doing something wrong, either with rand() or with my array declaration. Can someone please correct me:

my @numbers = [48..57, 97..112, 65..90];

$num = int(rand(@numbers));
3
Contributors
3
Replies
4
Views
8 Years
Discussion Span
Last Post by KevinADC
0

The problem is you are generating a random number based on the length of the array, not the values of the elements of the array. Here is what you want to do:

my @numbers = (48..57, 65..90, 97..112);
my $num = $numbers[int rand @numbers];
print $num,"\n";
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.