alih110 has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I have 2 perl processes which are running one after the other through a Makefile. During each run I would like to see both the perl processes get the same seed. A unique seed needs to be generated in each run. Both the perl processes are the same perl file run in 2 threads but sequentially. For example: This is what I would like: Run 1 : Perl process 0 generates seed 1446 and Perl process 1 also generates the same seed Run 2 : Perl process 0 generates seed 1245 and Perl process 1 also generates the same seed and so on..... I tried using time() function in my perl program but each perl process got a different seed with that. Any ideas how I can get both of them to get the same seed?
  • Comment on Generating same seed between 2 processes

Replies are listed 'Best First'.
Re: Generating same seed between 2 processes
by ikegami (Patriarch) on May 15, 2008 at 04:51 UTC
    You'd need to pass the same seed to srand in both processes. You could generate the seed in the parent and pass it to the Perl scripts via the environment or an argument.
    $ set seed=$RANDOM # Or whatever $ perl -e'$,=" "; $\="\n"; srand($ARGV[0]); print rand, rand, rand;' $ +seed 0.17082803610629 0.749901980484964 0.0963716556235674 $ perl -e'$,=" "; $\="\n"; srand($ARGV[0]); print rand, rand, rand;' $ +seed 0.17082803610629 0.749901980484964 0.0963716556235674
Re: Generating same seed between 2 processes
by BrowserUk (Patriarch) on May 15, 2008 at 04:52 UTC

    Add a third process that generates your random seed, and then supplies it to the two existing processes via their command lines:

    perl -e"$s= rand; qx[$argv[0] $s]; qx[ $ARGV[1] $s];" script1.pl scrip +t2.pl

    Switch command line conventions as appropriate.


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Generating same seed between 2 processes
by moritz (Cardinal) on May 15, 2008 at 07:49 UTC
Re: Generating same seed between 2 processes
by toolic (Bishop) on May 15, 2008 at 13:30 UTC
    Same thing, but using an environment variable.

    Here is the Makefile:

    > cat Makefile SEED := ${shell perl -e 'print int rand 10_000'} .EXPORT_ALL_VARIABLES: .PHONY: all all: @perl proc0 @perl proc1

    Here is the 1st script:

    > cat proc0 use warnings; use strict; if (exists $ENV{SEED}) { srand $ENV{SEED}; } else { die "Error: need a seed.\n"; } print "$0 ", rand, "\n";

    Here is the 2nd script:

    > cat proc1 use warnings; use strict; if (exists $ENV{SEED}) { srand $ENV{SEED}; } else { die "Error: need a seed.\n"; } print "$0 ", rand, "\n";

    Now run it a few times:

    > make proc0 0.0394185183840854 proc1 0.0394185183840854 > make proc0 0.821407354044826 proc1 0.821407354044826 > make proc0 0.697487873098911 proc1 0.697487873098911