Reviewing the docs and the source I think I can explain what the deal is. Hashes arent "randomized" at all. In fact from what I can tell all perls default to using the exact same hashing at the beginning. But when a hash goes pathological (which appears from the hash testing code to be defined as having twenty keys in a single bucket) then randomization kicks in, on a per hash basis.

I dont know if this behaviour makes sense, or if it matches with how its documented, but that appears to be the way it works.

The code below shows how you can put 10 keys in a single bucket but that twenty will cause the hash to randomize itself. I had to change tyes routine a little as the strings we are dealing with are all zero bytes (which effectively guarantees that they go in the same bucket in a non randomized hash).

I guess you could use this technique to randomize a hash. Stuff it with "\0" x 1 to "\0" x 20 and then empty it and then refill it with what you really want to store in it.

my %hash= map { "\0" x $_ => $_ }1..10; print "Ten keys:\n"; for my $av ( getKeyCollisions_Len( \%hash ) ) { print "@$av\n"; } print "Twenty keys:\n"; %hash= map { "\0" x $_ => $_ }1..20; for my $av ( getKeyCollisions_Len( \%hash ) ) { print "@$av\n"; } sub getKeyCollisions_Len { my( $hv )= @_; no warnings 'numeric'; my $buckets= 0 + %$hv; my @keys= keys %$hv; my( @clash, @return ); while( @keys ) { my $key= shift @keys; delete $hv->{$key}; my $b= 0 + %$hv; if( $b == $buckets ) { push @clash, length $key; } elsif( @clash ) { push @return, [@clash,length $key]; @clash= (); } $buckets= $b; } return @return; } __END__ Ten keys: 10 9 8 7 6 5 4 3 2 1 Twenty keys: 17 12 4 5 9 14
---
$world=~s/war/peace/g


In reply to Re: Hashes aren't being differently randomized (explanation) by demerphq
in thread Hashes aren't being differently randomized by Hue-Bond

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.