How can I get random number from text?
function text_to_number($text, $min, $max)
{
....
mt_rand($min, $max);
....
return $random_number;
}
$rand1 = text_to_number("text1", 1, 1000);
$rand2 = text_to_number("text2", 1, 1000);
So that “text1” always return 476 (for example) in mt_rand(1, 1000), and “text2” – always 241.
Is it possible?
What you want to do is to seed random generator with value based on your string. See, the code
will always assign the same value to
$value, no matter how many times you call it. Even better, all other calls to mt_rand will return the same values (different from each other) consistently from one run to another but you shouldn’t care about that.So, you have to write
How do you calculate seed based on text is another problem. You can convert each char to integer and sum them up. You can calculate CRC32. You can make MD5 and get part of that. Lots of choices here.
But really, why don’t you just use some sort of CRC or MD5 directly?