Thursday, January 10, 2013

Clock based seed in GAMS

If you use a random numbers in GAMS, setting execseed to a clock based number can be used to make each random number sequence different. I sometimes see:

execseed = gmillisec(jnow);

The idea is to initialize the seed with the milliseconds part of the current date. This is not completely correct however. In very rare cases this will fail with the message:

**** Exec Error at line 4: Random seed has to be > 0.0 and < 10**9

A better version looks like:

execseed = 1+gmillisec(jnow);

I.e. the advice given by the GAMS people here is actually not that good:


Note that runs with this clock based construct will make your runs irreproducable (and thus bugs difficult to handle).

1 comment:

  1. Thanks for the handy tip.
    To make the runs reproducible, you can save the random seed into a file, and then you can manually set the random seed to the stored value in later runs.

    scalar gams_random_seed;
    gams_random_seed = 1 + gmillisec(jnow);
    execseed = gams_random_seed;

    file gams_random_seed_file /gams_random_seed/;
    gams_random_seed_file.pc = 5
    put gams_random_seed_file;
    put gams_random_seed;