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...
convert string to pseudorandom array offset? (hashcode-ish)
Moderator: General Moderators
Re: convert string to pseudorandom array offset? (hashcode-ish)
If the number is too large you need to cut down the sample size.
I'd hash the name using something like md5 or sha1. Then take a sample of that hash, treat it as a base-16 number, modulo by the number of output names, ???, and profit.
2^32-1 ends up as eight Fs, 2^64-1 as 16. Depending on your system, pick a specific 8 or 16 digit chunk of the hash and do the base conversion.
I'd hash the name using something like md5 or sha1. Then take a sample of that hash, treat it as a base-16 number, modulo by the number of output names, ???, and profit.
2^32-1 ends up as eight Fs, 2^64-1 as 16. Depending on your system, pick a specific 8 or 16 digit chunk of the hash and do the base conversion.
Re: convert string to pseudorandom array offset? (hashcode-ish)
Thanks! That's pretty much exactly what I wanted!
One question, since you seem pretty "bit" wise... when I took 8 digits, sometimes the resulting modulo would produce a negative result.
I figured this was because sometimes there'd be a binary 1 in the negatives place...
one solution, then, might be to grab 7 rather than 8 heximal digits, for stuffing into hexdec() ?
But then I realized things are even odder in how PHP must be displaying vs. internally representing large ints...
something LOOKS like a large, positive int, but then the modulo arithmetic comes out negative!
Produces
Odd, that. But not too scary.
So I guess the final (semi-obfuscated) code is (assuming an array called $options) is $options[abs(intval(hexdec(substr(md5($text),0,8)))) % sizeof($options)];
Thanks!
One question, since you seem pretty "bit" wise... when I took 8 digits, sometimes the resulting modulo would produce a negative result.
I figured this was because sometimes there'd be a binary 1 in the negatives place...
one solution, then, might be to grab 7 rather than 8 heximal digits, for stuffing into hexdec() ?
But then I realized things are even odder in how PHP must be displaying vs. internally representing large ints...
something LOOKS like a large, positive int, but then the modulo arithmetic comes out negative!
Code: Select all
<?php
$a = 2520659895;
print "a is $a<br>";
$b = $a % 250;
print "b is $b<br>";
$c = abs(intval($a));
print "c is $c<br>";
$d = $c % 250;
print "d is $d<br>";
?>Code: Select all
a is 2520659895
b is -151
c is 1774307401
d is 151So I guess the final (semi-obfuscated) code is (assuming an array called $options) is $options[abs(intval(hexdec(substr(md5($text),0,8)))) % sizeof($options)];
Thanks!