How can I use reproducible randomization in Perl?

Tags: perl random
By : David B

I have a Perl script that uses rand to generate pseudorandom integers in some range. I want it to be random (i.e. not set the seed by myself to some constant), but also want to be able to reproduce the results of a specific run if needed.

What would you do?

By : David B


Your goals are at odds with each other. One one hand, you want a self-seeding, completely random sequence of integers; on the other hand, you want reproducibility. Completely random and reproducibility are at odds with each other.

You can set the seed to something you want. Possibly you want to have a default randomly determined seed, that will give you complete randomness when desired, but which can be set prior to a run manually to give reproducibility.

Log the seed for each run and provide a method to call the script and set the seed?

By : Oesor

Why don't you want to set the seed, but at the same time set the seed? As I've said to you before, you need to explain why you don't want to do something so we know what you are actually asking.

You might just set it yourself only in certain conditions:

srand( $ENV{SOME_SEED} ) if defined $ENV{SOME_SEED};

If you don't call srand, rand calls it for you automatically but it doesn't report the seed that it used (at least not until Perl 5.14).

It's really just a simple programming problem. Just turn what you outlined into the code that does what you said.

This video can help you solving your question :)
By: admin