Hey guys,
Maybe this community can help me once again.
I have a strange-ish request so I will try and explain very carefully.
I want to create a function that will return 1 digit...either 1,2,3 or 4.
the function will accept one argument which is always a string.
The string could be any length...probably never over 50 characters though.
What the function uses the string for is as a randomizing factor in choosing whether to return 1,2,3 or 4.
So this is an example setup:
$string1 = "sheep";
$digit = function-i-need($string1);
echo $digit;
and let's say that the number 3 is printed.
A second example:
$string2 = "sheep";
$digit = function-i-need($string2);
echo $digit;
and let's say that the number 1 is printed.
There is one thing which is stopping this from being a simple random-number-between-1-and-4 function and that is: if the same string is input into the function twice then the same digit must always be returned.
If you were to feed 20 different strings through this function it would be nice if on average 5 of each digit between 1 and 4 were returned...it doesn't have to be exact by any means though.
e.g.
Maybe you feed 20 strings through the function and you get nine 1's, three 2's, five 3's and three 4's. I would be happy with that.
I thought of one way of doing it which is to convert the strings into long numbers and then take the last digit as long as it is 1,2,3 or 4...and if it isn't then try the second number and so on.
And if there isn't a 1,2,3 or 4 in the long number(very rare) then just output number 1 or something.
I am not certain about how to build my method either and I feel there must be a better and more efficient way of doing this...is there?
Any help would be greatly appreciated.
p.s. the function needs to be relatively efficient as it will be called at least 20 times per page load on my site...the site gets around 11,000 page loads a day.