Movatterモバイル変換


[0]ホーム

URL:


functions /rand
(source,CPAN)
You are viewing the version of this documentation from Perl 5.42.0-RC3. This is a development version of Perl.
#rand EXPR
#rand

Returns a random fractional number greater than or equal to0 andless than the value of EXPR. (EXPR should be positive.) If EXPR is omitted, the value1 is used.

my $num1 = rand();        # Random float at least 0 and below 1my $num2 = rand(7);       # Random float at least 0 and below 7my $num3 = int(rand(10)); # Random integer at least 0 and below 10

Notes:

  • Callingrand automatically callssrand unless srand has already been called.

  • Currently EXPR with the value0 is special-cased as1. This was undocumented before Perl 5.8.0 and is subject to change in future versions of Perl.

  • As of Perl v5.20.0rand() uses thedrand48 pseudorandom number generator to generate random numbers. As a PRNGdrand48 should be sufficient for most non-cryptographic needs. If you need cryptographic random numbers check CPAN for crypto safe alternatives.

#Security:

rand is not cryptographically secure. You should not rely on it in security-sensitive situations. As of this writing, a number of third-party CPAN modules offer random number generators intended by their authors to be cryptographically secure, including:

Perldoc Browser is maintained by Dan Book (DBOOK). Please contact him via theGitHub issue tracker oremail regarding any issues with the site itself, search, or rendering of documentation.

The Perl documentation is maintained by the Perl 5 Porters in the development of Perl. Please contact them via thePerl issue tracker, themailing list, orIRC to report any issues with the contents or format of the documentation.


[8]ページ先頭

©2009-2025 Movatter.jp