larger seeds for Mersenne

Tim Peters tim.one at comcast.net
Thu Feb 5 00:45:10 EST 2004


[jt]
> I'm trying to learn how to use a seed for the random module that is
> larger than the integer produced by python's built-in hash(x)
> function.
>
> From the documentation, it says:
>
> "If x is not None or an int or long, hash(x) is used instead. If x is
> an int or long, x is used directly."
>
> So if I through something other than an int or long, it gets hashed
> down to an int.

Right.

> Which is what, 10 bytes or so?

Generally 4 bytes on a 32-bit machine, or 8 bytes on a 64-bit machine.  It's
the same as the platform C long int.

> So it seems like it would be preferable to use something like md5 or
> sha to hash because then I'd get 16 or 20 bytes for the seed.  But the
> problem is that md5 and sha don't hash to a long.  They include lots of
> other printable and nonprintable characters.

The .hexdigest() methods are what you're looking for.

> Does anyone know of a hashing algorithm that hashes to a long that's
> 16-20+ bytes long?  Thanks for any help.

>>> import sha
>>> sha.new('tim').hexdigest()
'5ee0edb9e2229c0838f1959779f1949031de0123'
>>> int(_, 16)
541661208246500712261618375284272868275380945187L
>>> md5.new('tim').hexdigest()
'b15d47e99831ee63e3f47cf3d4478e9a'
>>> int(_, 16)
235757697420119658189499990054961385114L
>>>





More information about the Python-list mailing list