in reply to Re: perl rand() function
in thread perl rand() function

func,

This isn't gonna work. If I want random from 50 - 100, you code puts 50 to 100 in an array then chooses random indexes from 0 - 99. But there are only 51 items in the array. To fix this change your rand parameter from $highest_number to @number_range.

Here's another way:

YuckFoo

#!/usr/bin/perl use strict; my ($beg, $end) = qw(10 20); my $range = ($end - $beg) + 1; for (1..6) { print int(rand($range) + $beg) . "\n"; }

Replies are listed 'Best First'.
Re: Re: Re: perl rand() function
by func (Acolyte) on Apr 17, 2002 at 20:38 UTC

    Thanks. I never knew rand() could have an array as an expression. Also, I'll have to do more testing before I post my code. :)