Skip to content

Commit dde110a

Browse files
scottchiefbakerkhwilliamson
authored andcommitted
Update the rand() documentation to make it clearer
1 parent 41e43bc commit dde110a

File tree

1 file changed

+47
-21
lines changed

1 file changed

+47
-21
lines changed

pod/perlfunc.pod

Lines changed: 47 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -6561,32 +6561,61 @@ X<rand> X<random>
65616561

65626562
=for Pod::Functions retrieve the next pseudorandom number
65636563

6564-
Returns a random fractional number greater than or equal to C<0> and less
6564+
Returns a random fractional number greater than or equal to C<0> and B<less>
65656565
than the value of EXPR. (EXPR should be positive.) If EXPR is
6566-
omitted, the value C<1> is used. Currently EXPR with the value C<0> is
6567-
also special-cased as C<1> (this was undocumented before Perl 5.8.0
6568-
and is subject to change in future versions of Perl). Automatically calls
6569-
L<C<srand>|/srand EXPR> unless L<C<srand>|/srand EXPR> has already been
6570-
called. See also L<C<srand>|/srand EXPR>.
6566+
omitted, the value C<1> is used.
65716567

6572-
Apply L<C<int>|/int EXPR> to the value returned by L<C<rand>|/rand EXPR>
6573-
if you want random integers instead of random fractional numbers. For
6574-
example,
6568+
my $num1 = rand(); # Random float at least 0 and below 1
6569+
my $num2 = rand(7); # Random float at least 0 and below 7
6570+
my $num3 = int(rand(10)); # Random integer at least 0 and below 10
65756571

6576-
int(rand(10))
6572+
B<Notes:>
65776573

6578-
returns a random integer between C<0> and C<9>, inclusive.
6574+
=over
6575+
6576+
=item *
6577+
6578+
Calling C<rand> automatically calls L<C<srand>|/srand EXPR> unless
6579+
srand has already been called.
6580+
6581+
=item *
6582+
6583+
Currently EXPR with the value C<0> is special-cased as C<1>. This was
6584+
undocumented before Perl 5.8.0 and is subject to change in future versions of
6585+
Perl.
6586+
6587+
=item *
6588+
6589+
As of Perl v5.20.0 C<rand()> uses the C<drand48> pseudorandom number generator
6590+
to generate random numbers. As a PRNG C<drand48> should be sufficient for most
6591+
non-cryptographic needs. If you need cryptographic random numbers check CPAN
6592+
for crypto safe alternatives.
6593+
6594+
=back
6595+
6596+
=over
65796597

6580-
(Note: If your rand function consistently returns numbers that are too
6581-
large or too small, then your version of Perl was probably compiled
6582-
with the wrong number of RANDBITS.)
6598+
=item B<Security:>
65836599

65846600
B<L<C<rand>|/rand EXPR> is not cryptographically secure. You should not rely
65856601
on it in security-sensitive situations.> As of this writing, a
65866602
number of third-party CPAN modules offer random number generators
65876603
intended by their authors to be cryptographically secure,
6588-
including: L<Data::Entropy>, L<Crypt::Random>, L<Math::Random::Secure>,
6589-
and L<Math::TrulyRandom>.
6604+
including:
6605+
6606+
=back
6607+
6608+
=over
6609+
6610+
=item * L<Data::Entropy>
6611+
6612+
=item * L<Crypt::Random>
6613+
6614+
=item * L<Math::Random::Secure>
6615+
6616+
=item * L<Math::TrulyRandom>
6617+
6618+
=back
65906619

65916620
=item read FILEHANDLE,SCALAR,LENGTH,OFFSET
65926621
X<read> X<file, read>
@@ -8951,11 +8980,8 @@ startup, and changing it during the program flow will not affect the
89518980
currently running process. See L<perlrun> for more details.
89528981

89538982
B<L<C<rand>|/rand EXPR> is not cryptographically secure. You should not rely
8954-
on it in security-sensitive situations.> As of this writing, a
8955-
number of third-party CPAN modules offer random number generators
8956-
intended by their authors to be cryptographically secure,
8957-
including: L<Data::Entropy>, L<Crypt::Random>, L<Math::Random::Secure>,
8958-
and L<Math::TrulyRandom>.
8983+
on it in security-sensitive situations.> See documentation of C<rand> for
8984+
a list of suitable alternatives.
89598985

89608986
=item stat FILEHANDLE
89618987
X<stat> X<file, status> X<ctime>

0 commit comments

Comments
 (0)