Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re^5: How do I make a random shuffle deterministic?

by Anonymous Monk
on Dec 06, 2012 at 15:07 UTC ( #1007599=note: print w/replies, xml ) Need Help??


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 (Bishop) 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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007599]
help
Chatterbox?
[Corion]: marto: Ow, no, that doesn't help at all :)
[choroba]: Related to the new release, anyone could explain this or this tester report?
[Discipulus]: hello crew! marto thanks for the message: but I how can I help? i'm testing cpan Padre atm problem with Client::Debug
[choroba]: I don't happen to have 5.10.0 nor 5.8.5 handy...
[Corion]: Hmm - I would say the 5.8.5 is a broken installation / corrupt tarball download, and the 5.10.0 is really weird, and maybe a bug in that version of Perl
[Corion]: I don't see how my $result = eval q{'abc' =~ ?b?}; could create a "Modification of read-only value" error

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (10)
As of 2018-06-25 08:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?