David B
David B

Reputation: 30018

How can I use reproducible randomization in Perl?

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?

Upvotes: 2

Views: 1075

Answers (5)

Paul Sonier
Paul Sonier

Reputation: 39520

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.

Upvotes: 0

Sinan Ünür
Sinan Ünür

Reputation: 118166

If the purpose is to be able to reproduce simulation paths which incorporate random shocks (say, when you are running an economic model to produce projections, I would give up on the idea of storing the seed, but rather store each sequence alongside the model data.

Note that the built in rand is subject to vagaries of the rand implementation provided by the C runtime. On all Windows machines and across all perl versions I have used, this usually means that rand will only ever produce 32768 unique values.

That is severely limited for any serious purpose. In simulations, a crucial criterion is that random sequences used be independent of each other so that each run can be considered an independent realization.

In fact, if you are going to run a simulation 1,000 times, I would pre-produce 1,000 corresponding random sequences using known-good generators that are consistent across platforms and store them with the model inputs.

You can update the simulations using the same sequences or a new set if parameter estimates change when you get new data.

Upvotes: 1

brian d foy
brian d foy

Reputation: 132914

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.

Upvotes: 1

Cascabel
Cascabel

Reputation: 497752

McWafflestix says:

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.

The obvious way to implement this is to follow your normal seeding process (either manually from a strong random source, or letting perl do it automatically on the first call to rand), then use the first generated random value as the seed, and record it. If you want to reproduce later, just use a recorded value for the seed.

# something like this?

if ( defined $input_rand_seed ) {
    srand($input_rand_seed);
} else {
    my $seed = rand();   # or something fancier
    log_random_seed($seed);
    srand($seed);
}

Upvotes: 2

Oesor
Oesor

Reputation: 6642

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

Upvotes: 0

Related Questions