GitXplorerGitXplorer
s

perl-Random-Simple

public
2 stars
1 forks
2 issues

Commits

List of commits on branch main.
Unverified
05e1fd1eb63fdb9f00591c07f709a251da8bbd7c

Explain what a good seed is

sscottchiefbaker committed 2 days ago
Unverified
cc8851c8216e3cd1a0407fa1fce1885f1a85ab2c

We do not use the seed so don't output it (it's noisy)

sscottchiefbaker committed 2 days ago
Unverified
d9490ae617c5948242673916d53bcbd6e4da8218

Add tests for random_elem()

sscottchiefbaker committed 2 days ago
Unverified
8aa80ad69aa48ea33e89642ace179c5a01df62a0

Add srand() tests

sscottchiefbaker committed 2 days ago
Unverified
9486394ea74a481f40fdf3e89133af6742e185d3

Seeds should be ints()

sscottchiefbaker committed 2 days ago
Unverified
d76e48064975867d928730eda13f97c40d812191

Make our `srand()` more consistent with CORE

sscottchiefbaker committed 2 days ago

README

The README file for this repository.

NAME

Random::Simple - Generate good random numbers in a user consumable way.

SYNOPSIS

use Random::Simple;

my $coin_flip      = random_int(1, 2);
my $die_roll       = random_int(1, 6);
my $random_percent = random_float() * 100;
my $buffer         = random_bytes(8);

my @arr            = ('red', 'green', 'blue');
my $rand_item      = random_elem(@arr);

DESCRIPTION

Perl's internal rand() function uses drand48 which is an older pseudorandom number generator and may have limitations. Random::Simple uses PCG which is: modern, simple, well vetted, and fast. Using Random::Simple will automatically upgrade/override the core rand() function to use a better PRNG.

Random::Simple is automatically seeded with entropy directly from your OS. On Linux this is /dev/urandom and on Windows it uses CryptGenRandom. You will get statistically unique random numbers automatically.

METHODS

  • random_int($min, $max)

    returns a non-biased integer between $min and $max (inclusive). Range must be no larger than 2**32 - 2.

  • random_float()

    returns a random floating point value between 0 and 1 (inclusive).

  • random_bytes($number)

    returns a string of random bytes with length of $number.

  • random_elem(@array)

    returns a random element from @array.

  • srand()

    emulates CORE::srand() using a better PRNG.

  • rand()

    emulates CORE::rand() using a better PRNG.

  • Random::Simple::seed($seed1, $seed2)

    Seed the PRNG with two unsigned 64bit integers for predictable and repeatable random numbers. Random::Simple will automatically seed itself from your operating system's randomness if not manually seeded. Manual seeding should only be used in specific cases where you need repeatable or testable randomness.

CAVEATS

PCG uses two 64bit unsigned integers for seeding. High quality seeds are needed to generate good random numbers. Random::Simple automatically generates high quality seeds by reading random bytes from your operating system and converting appropriately.

If you manually seed Random::Simple, then make sure you use good seeds that are mostly non-zero. The larger the number the better seed it will make. A good seed is a decimal number with 18 or 19 digits.

BUGS

Submit issues on Github: https://github.com/scottchiefbaker/perl-Random-Simple/issues

SEE ALSO

AUTHOR

Scott Baker - https://www.perturb.org/