Perl Scripts ·
Random Numbers in Perl with rand() and int()
By Manu Mathew, Technical Lead Engineer · Published 16 December 2013 · Last updated 5 October 2026

Dice games, sample data and simple load spreading all need random numbers in Perl. The built-in rand() covers them, with one important exception: anything to do with security.
Quick answer
rand() returns a fraction from 0 up to (but not including) 1. rand(N) returns a fraction from 0 up to N. For whole numbers, wrap it in int(): int(rand(6)) + 1 gives 1 to 6. Don’t use rand() for passwords, tokens or anything security-related.
How do I get numbers in a range?
use strict;
use warnings;
my $fraction = rand(); # 0 <= $fraction < 1
my $up_to_100 = rand(100); # 0 <= $up_to_100 < 100
my $dice = int(rand(6)) + 1; # whole number from 1 to 6
my ($min, $max) = (100, 150);
my $in_range = $min + int(rand($max - $min + 1)); # 100 to 150, both included
The + 1 in $max - $min + 1 is what makes $max itself possible.
How do I pick a random element from a list?
my @colours = qw(red green blue);
my $pick = $colours[rand @colours]; # array index is truncated to a whole number
Do I need to call srand()?
Usually not. rand() calls srand() automatically the first time it is used. Call srand($fixed_number) yourself only when you want the same sequence every run, for example in tests.
Can I use rand() for passwords or tokens?
No. rand() is predictable enough that it should not be used for security. The Perl documentation suggests CPAN modules such as Crypt::URandom, Crypt::SysRandom or Crypt::PRNG instead.
Frequently asked questions
How do I shuffle a list into a random order?
Use shuffle from List::Util, a core module: my @deck = shuffle @cards;. It returns the elements in a random order and leaves the original array as it was. Like rand(), it isn’t suitable for anything security-related.
How do I make a random reference code?
Pick characters from a list with rand: join '', map { $chars[rand @chars] } 1 .. 8 gives eight random characters. That is fine for a display reference. For passwords, tokens or anything a person could guess to gain access, use a cryptographically secure generator instead.
Why do I get the same numbers every time I run the script?
Something is calling srand with a fixed value, often left over from testing. Remove that call, and Perl seeds the generator itself the first time rand runs, so each run gives a different sequence.
Is rand() good enough for simulations or sampling?
For games, demos, test data and simple sampling, yes. Since Perl 5.20, rand() uses the same built-in generator on every platform, so results behave consistently. For serious statistical work, use a dedicated generator from CPAN and record which one you used.
Related notes: Perl arrays: length, add, remove and search; dates in Perl; checking whether a Perl variable is a number.
Related reading: Perl strings and regex; Sorting arrays in Perl; hiring a remote Perl developer.
Need help with Perl code?
If you have Perl code that needs fixing, extending or upgrading, I’m happy to take a look. I work remotely from Kozhikode, Kerala, with teams across India and elsewhere; Perl development and maintenance explains how I help. If you are deciding how to bring someone in, Hiring a remote Perl developer is a short checklist.
You can reach me through the contact section or email me at manu@manu.co.in. A few lines about the system and what you need is a good start.
An earlier version of this note reproduced parts of a perlmeme.org tutorial on rand(). This version is rewritten in my own words, with new examples.
Sources: perlfunc: rand; perlfunc: srand; perlmeme.org (link removed: the site currently fails over HTTPS).