Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

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

by LanX (Chancellor)
on Dec 05, 2012 at 17:15 UTC ( #1007340=note: print w/replies, xml ) Need Help??

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

Most implementations of "srand" take an integer and will silently truncate decimal numbers. This means "srand(42)" will usually produce the same results as "srand(42.1)". To be safe, always pass "srand" an integer.
most strings will evaluate to 0 !
DB<116> srand 0 => 1 DB<117> rand => "0.17082803610629" DB<118> rand => "0.749901980484964" DB<119> srand hostname => 1 DB<120> rand => "0.17082803610629" DB<121> rand => "0.749901980484964" DB<122> 0+ hostname => 0
You need a checksum to produce a (pseudo) unique integer. You could try

$seed += ord($_) for split //, hostname

But whats wrong with the IP?

Cheers Rolf

Replies are listed 'Best First'.
Re^3: How do I make a random shuffle deterministic?
by Anonymous Monk on Dec 05, 2012 at 17:32 UTC

    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.

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

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

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1007340]
[TStanley]: I'm sitting in the airport in Tampa, Florida at the moment, getting ready to head home.
[talexb]: TStanley I'm flying to Vegas in two weeks time. After that flights leaving Vegas and Phoenix are late at night .. I've been hearing that it gets so hot that the air isn't dense enough for planes to take off. Weird. Guess you won't have that problem.
[Corion]: Yay, vacation time now - one week off work (but a Perl workshop in the middle ;)) )
[talexb]: TStanley I just realized that I was in your area for a convention back in April. We were at the high school for the weekend, the one with the long driveway. Nice spot.

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (8)
As of 2017-06-23 14:15 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (548 votes). Check out past polls.