For cases when you can't, or don't want to, rely on an external module, this is quick, simple, and good enough for non scientific computing:
sub make_random_generator {
my ($seed, $min, $max) = @_;
# DO NOT CHANGE THESE
my $a = 7 ** 5;
my $m = (2 ** 31) - 1;
# if seed was zero this generator will return zeros
# forever, set it to some arbitrary value
if ($seed == 0) { $seed = 12345678; }
unless (defined $min) { $min = 0; }
unless (defined $max) { $max = $m; }
if ($max < $min) {
my $t = $min;
$min = $max;
$max = $t;
}
return sub {
$seed = ($a * $seed) % $m;
# "scale" $seed to the range ($min,$max)
return int ((($seed / $m) * ($max - $min)) + $min);
}
}
use it like this:
for my $seq_n (1..10) {
print "random sequence with seed $seq_n:\n";
my $gen = make_random_generator(rand, 0, 1000);
print join ", ", map { $gen->() } 1 .. 10;
print "\n";
}
this will return the same 10 pseudo random sequences every time you run it.