Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Hashes aren't being differently randomized (explanation)

by demerphq (Chancellor)
on Jun 28, 2006 at 07:58 UTC ( [id://557962]=note: print w/replies, xml ) Need Help??

in reply to Hashes aren't being differently randomized

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

Log In?

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-22 21:26 GMT
Find Nodes?
    Voting Booth?

    No recent polls found