Random Numbers in Perl

Here’s the translation of the Go code to Perl, with explanations in Markdown format suitable for Hugo:

Perl’s rand function provides pseudorandom number generation.

use strict;
use warnings;

# For example, int(rand(100)) returns a random integer n,
# 0 <= n < 100.
print int(rand(100)), ",";
print int(rand(100)), "\n";

# rand() returns a float f,
# 0.0 <= f < 1.0.
print rand(), "\n";

# This can be used to generate random floats in
# other ranges, for example 5.0 <= f' < 10.0.
print (rand(5) + 5), ",";
print (rand(5) + 5), "\n";

# If you want a known seed, use srand().
# srand() sets the seed for rand().
srand(42);
print int(rand(100)), ",";
print int(rand(100)), "\n";

# Using the same seed will produce the same sequence of random numbers.
srand(42);
print int(rand(100)), ",";
print int(rand(100)), "\n";

Some of the generated numbers may be different when you run the sample.

$ perl random-numbers.pl
68,56
0.8090228139659177
5.840125017402497,6.937056298890035
94,49
94,49

See the rand function documentation for references on other random quantities that Perl can provide.

In Perl, the rand() function generates random floating-point numbers between 0 and 1. To get integers, we use int(rand(max)) which returns an integer from 0 to max-1.

Unlike Go, Perl doesn’t have built-in support for different random number generators. The srand() function is used to set a seed for the random number generator, which is useful for reproducibility.

If you need more advanced random number generation capabilities, you might want to look into Perl modules like Math::Random::MT for Mersenne Twister algorithm or Math::Random::Secure for cryptographically secure random numbers.