Returns a random fractional number greater than or equal to 0 and less than the value of EXPR. (EXPR should be positive.) If EXPR is omitted, the value 1 is used.
my $num1 = rand(); # Random float at least 0 and below 1
my $num2 = rand(7); # Random float at least 0 and below 7
my $num3 = int(rand(10)); # Random integer at least 0 and below 10
Notes:
Calling rand automatically calls srand unless srand has already been called.
Currently EXPR with the value 0 is special-cased as 1. This was undocumented before Perl 5.8.0 and is subject to change in future versions of Perl.
As of Perl v5.20.0 rand() uses the drand48 pseudorandom number generator (PRNG) to generate numbers that appear random but are deterministic. It is sufficient for games, generating test data or cases where repeatability is needed.
rand is not suitable for anything related to security, authentication or encryption. Do not use it for generating passwords, session ids, authentication tokens, salts, or encryption keys. The return values are predictable, and systems that rely on rand are easily broken.
A number of third-party CPAN modules offer access to system sources of randomness, or random number generators that are suitable for security purposes, including: