convert string to pseudorandom array offset? (hashcode-ish)
Posted: Thu Dec 04, 2008 4:20 pm
Experienced coder, newbie at PHP...
So I'm doing one of those silly "name generators", where I have an array of possible names, and I would like to make a script so that for any given input (like "Fred") it returns the same output (like "the Doofus")
This is a hash-like function, but I think my webhost isn't quite up to date, and lacks spl_object_hash - but I'm not sure that is what I'm looking for anyway, if it returns the same value each time the php is loaded for the same string, or of its more a trick of memory management.
My thought was to treat the string as, in effect, a base-26 number, and then mod that by the number of array entries.
There are a few details to get right (to make sure that "0300" maps differently than "300" for instance) but I thought the idea was sound--
So, the problem is that this number gets large fairly quickly, large enough so that I can't use it with the % operator or for a seed for srand()
Any thoughts? I might have 500-100 values, and I'd like to get fairly even distribution over these values, but I do want it be consistent...
So I'm doing one of those silly "name generators", where I have an array of possible names, and I would like to make a script so that for any given input (like "Fred") it returns the same output (like "the Doofus")
This is a hash-like function, but I think my webhost isn't quite up to date, and lacks spl_object_hash - but I'm not sure that is what I'm looking for anyway, if it returns the same value each time the php is loaded for the same string, or of its more a trick of memory management.
My thought was to treat the string as, in effect, a base-26 number, and then mod that by the number of array entries.
There are a few details to get right (to make sure that "0300" maps differently than "300" for instance) but I thought the idea was sound--
So, the problem is that this number gets large fairly quickly, large enough so that I can't use it with the % operator or for a seed for srand()
Any thoughts? I might have 500-100 values, and I'd like to get fairly even distribution over these values, but I do want it be consistent...