id() trickery

Tim Peters tim_one at email.msn.com
Mon Mar 20 01:25:41 EST 2000


[Eric Jacobs, among much fine advice, stumbles]
> ...
> If you're looking for a number which will be equal among objects which
> are equal (not identical), you might want to look into the hash
> function. Assuming hash is defined,
>
> hash(a) == hash(b)    if and only if    a == b

That one only works one way:

    a == b implies hash(a) == hash(b)

but the converse doesn't hold.  For brownie points, figure out the expected
number of iterations before this program produces some output:

import random
import sys

def drive():
    hash2float = {}
    haskey = hash2float.has_key
    _hash, _random = hash, random.random
    for i in xrange(sys.maxint):
        x = _random()
        h = _hash(x)
        if haskey(h):
            if hash2float[h] != x:
                print "collision on hash code", h, \
                      "%24.17g %24.17g" % (x, hash2float[h]), \
                      "at i =", i
                break
        else:
            hash2float[h] = x

drive()

On a 32-bit machine, here's sample output from two different runs:

collision on hash code -1892459086      0.57216902940840741
                                        0.19135372926869554 at i = 98506

collision on hash code -757800254      0.39944426781689946
                                       0.72798761861358496 at i = 52767

you're-halfway-to-becoming-a-cryptographer<wink>-ly y'rs  - tim






More information about the Python-list mailing list