develooper Front page | perl.perl5.porters | Postings from April 2001

[DOC PATCH bleadperl] Document generation of random integers

Thread Next
From:
Walt Mankowski
Date:
April 29, 2001 18:53
Subject:
[DOC PATCH bleadperl] Document generation of random integers
Message ID:
20010429215348.A3971@netaxs.com
A beginner was asking how to do this on clpm yesterday.  Adding this
patch:

* Documents how to do it.

* Saves the beginner the trouble of hunting through perlfunc to figure
  out how to truncate floating point numbers.

* Shows the range which is returned (which might not be intuitive).

Walt


--- pod/perlfunc.pod.orig	Thu Apr  5 18:54:57 2001
+++ pod/perlfunc.pod	Sun Apr 29 21:19:58 2001
@@ -3509,6 +3509,13 @@
 omitted, the value C<1> is used.  Automatically calls C<srand> unless
 C<srand> has already been called.  See also C<srand>.
 
+Apply C<int()> to the value returned by C<rand()> if you want random
+integers instead of random fractional numbers.  For example,
+
+    int(rand(10))
+
+returns a random integer between C<0> and C<9>, inclusive.
+
 (Note: If your rand function consistently returns numbers that are too
 large or too small, then your version of Perl was probably compiled
 with the wrong number of RANDBITS.)

Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About