Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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

by Anonymous Monk
on Dec 05, 2012 at 17:32 UTC ( #1007342=note: print w/ replies, xml ) Need Help??


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

The script has be as portable as possible. I'm not certain that the modules required to get an IP address will be available on an old version of perl that might be found on a Solaris or AIX host.


Comment on Re^3: How do I make a random shuffle deterministic?
Re^4: How do I make a random shuffle deterministic?
by LanX (Canon) on Dec 05, 2012 at 17:37 UTC
    I just realized you had the same idea. But

    1. The sum of two strings can be identical, try "rolf" and "rofl" and "flor"

    2. for ( < @array > ) is dangerous nonsense, always do for (  @array  ) without glob

    3. if you wanna play save, try a system cmd to get a mac adress.

    4. You could determine a unique seed at installation time and store it in a module MyProject::Seed you use later. Like this you have full control (just edit the module) and you only need to fiddle once with the OS for a unique key.

    5. you could even request a unique key over web at installation time.

    Cheers Rolf

      Thanks for the tips. It is acceptable if some hosts have the same seed. Still, lessening those occurrences is a good thing. How about this:

      sub seed { my $string = shift; my $seed; my @ascii = map ord, split //, $string; my $product = pop @ascii; for ( @ascii ) { $seed += $_ }; $seed = $seed * $product; srand $seed; }
        "rolfi" and "flori"?

        just a guess: try multiplying each ord with a different prime (by position) and do a module biggest prime <256 before adding.

        or start a new thread about efficient checksums! :)

        UPDATE: the automatic seeding is quite efficient, so why don't you determine a unique seed at installation time from a random number.

        Cheers Rolf

      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 );
        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

Re^4: How do I make a random shuffle deterministic?
by DrHyde (Prior) on Dec 06, 2012 at 11:58 UTC
    If the modules aren't installed, then install them. If the most recent version of a module doesn't work on your ancient system, use cpXXXan.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007342]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (3)
As of 2014-12-29 02:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (184 votes), past polls