http://www.perlmonks.org?node_id=1007599


in reply to Re^4: How do I make a random shuffle deterministic?
in thread How do I make a random shuffle deterministic?

New try after some research.

use Sys::Hostname; use Digest::MD5 qw( md5 ); sub seed { my $string = shift; my $seed = 0; my $digest = md5( $string ); for ( split //, $digest ) { $seed = ( $seed * 256 + ord ) % 1e12; } srand $seed; } # main matter my $hostname = hostname; seed ( $hostname );

Replies are listed 'Best First'.
Re^6: How do I make a random shuffle deterministic?
by LanX (Saint) on Dec 06, 2012 at 15:31 UTC
    Looks fine for me!

    The modules are core and your producing a maximal seed.

    Still think it's exaggerated for your problem...

    ... but it's a good generic solution for similar tasks! =)

    My only concern is readability, better use something like ord $char with an explicit loop var my $char

    Anyway you are still operating on the assumption that hostnames are always different, since I don't know your project this might be ok or not.

    Generating a random seed at installation time would prevent this "problem".

    Cheers Rolf