16bit hash

Paul Rubin http
Wed Jun 27 22:57:20 EDT 2007


Robin Becker <robin at reportlab.com> writes:
> >> Is the any way to get an efficient 16bit hash in python?
> > hash(obj)&65535
> >   - Josiah
> yes I thought of that, but cannot figure out if the internal hash
> really distributes the bits evenly. Particularly since it seems to
> treat integers etc as special cases

The built-in hash function doesn't attempt even distribution, it tries
to make sure that inputs with small differences hash to distinct
values.  If you want something really near-random, try

    import sha
    hash = int(sha.new(repr(obj)).hexdigest()[:4], 16)

It won't be super-fast but hey, you're using Python.



More information about the Python-list mailing list