in reply to random number with range

Just use the rand function. It generates numbers in a half open interval ranging from (and including) 0 through to (but not including) the positive number given (or 1 if omited). The result is a real number (not an integer) in that range. If you want to pick an integer between $start and $end inclusive you can do this:

my $number = $start + int rand $end - $start + 1;

$end must be >= $start.


DWIM is Perl's answer to Gödel

Replies are listed 'Best First'.
Re^2: random number with range
by Anonymous Monk on Apr 04, 2020 at 19:28 UTC
    ok, that works , but why you add +1 in the end? i see that without it, the generator does not generate full lenght( te top number never apiers) but with +1 its all right, where does the 1 goes that we need manually add it?

      This might explain; rand(1) can never produce 1 for example, so–

      perl -E 'say int 0.999999999' 0