Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: random number with range

by GrandFather (Saint)
on Jun 01, 2006 at 17:34 UTC ( [id://553097]=note: print w/replies, xml ) Need Help??


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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://553097]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-24 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found