Welcome, guest | Sign In | My Account | Store | Cart

Convert strings to floats in the range [0, 1), using a hash function

Python, 7 lines
1
2
3
4
5
6
7
def hashed_float(s):
    """returns a float in the range [0, 1) based on a hash of the string.
    A given string will always return the same value, but different strings
    will return very different values."""
    import md5, struct
    [number] = struct.unpack("<H", md5.new(s).digest()[:2])
    return number / float(0xFFFF)

I use this for creating sample test suites where each test takes a different time to complete, using each test's name as the parameter.

2 comments

Josiah Carlson 19 years ago  # | flag

One could also use...

import random
random.seed(STRING)
val = random.random()

Or even bypass all modules entirely...

def r(STRING):
    return (hash(STRING)+2.0**31)/2**32
Ori Peleg (author) 18 years, 10 months ago  # | flag

True, but... random()'s or hash()'s results are not reproducible in other languages, nor necessarily in different versions of Python where implemenatations may change. MD5, or any other standard hash function, does guarantee reproducible results.