Function rand() in Perl

In the Perl programming language has a built-in function rand(). This function is used to generate random numbers.

Here's an example:

▶ Run
#!/usr/bin/perl

print rand(10);

This program displays a random number between 0 (inclusive) to 10 (but not including the number 10).

For example, this program may be the number 2.61509101678715.

Restriction

Function rand() returns a pseudorandom number. In some situations, under some conditions it is possible to predict what value returns the function rand(). From this it follows that this function is not cryptographically-strong. This function should not be used in cryptography, but to get some random values out of scope cryptography-this feature is ideal.

Arguments

my $value1 = rand();
my $value2 = rand($number);

Function rand() can be used without any arguments or give her exactly one argument. The function expects a number as argument value. If the function receives is not a number she leads him to the number.

Using the same argument

When using positive numbers as the argument of a function rand() returns the number of greater than or equal to 0 and strictly less than its argument. For example, if you use 100in as argument, you can get the number 72.9877832186506 as the function result rand().

You can use fractional arguments. If you specify rand(1.5), the result is a number greater than or equal 0 and strictly less 1.5.

When you use a negative number as the argument function returns a number which is strictly greater than the specified argument is less than or equal 0.

Use without arguments

Using rand() with no arguments is the same as that to use it with the argument 1. Function rand() will return a number which is greater than or equal 0 and strictly less 1, for example 0.813735968739824.

▶ Run
#!/usr/bin/perl

print rand(1);

Please note, if you do not specify an argument, the argument is the number 1, not the value of the variable $_. If you want to use $_ as the value rand(), it is necessary to write explicitly:

▶ Run
#!/usr/bin/perl

$_ = 3;

print rand($_);

0 as the argument values

The number 0 as the value argument of the function rand() is a special case. rand(0) is the same as rand(1). The function will return a number greater than or equal to 0 and strictly smaller 1.

The use of more than one argument

If the transfer function rand() more than one argument, it will error:

Too many arguments for rand at script.pl line 3, near "2)"
Execution of script.pl aborted due to compilation errors.

Official documentation

Here is the output of the command perldoc -f rand:

       rand EXPR
       rand    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.  Currently EXPR with
               the value 0 is also special-cased as 1 (this was undocumented
               before Perl 5.8.0 and is subject to change in future versions
               of Perl).  Automatically calls "srand" unless "srand" has
               already been called.  See also "srand".

               Apply "int()" to the value returned by "rand()" if you want
               random integers instead of random fractional numbers.  For
               example,

                   int(rand(10))

               returns a random integer between 0 and 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.)

               "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: Data::Entropy, Crypt::Random,
               Math::Random::Secure, and Math::TrulyRandom.

Other articles

Comments