I'm in need of this particular function for my program. Weighted random function which basically pick out a random element inside a array base on the weight of the element.
// Or it could be in another way.
$element_weight = array('one' => '0.5', 'two' => '0.65', 'three' => '0.8');
And base on the function, randomly select one element out from the array base on the probability that it might be chosen. I've did some research through the internet and found two that would possible be what I need. You could refer to Random-Weighted-Element-In-PHP and The Forge of the best Weighted Random function.
I thought that either one would matches what I need. And I'm not sure which one would better suit mine. And also, I have to tweak a little to suit my program. Anyone could give me advise on this or maybe suggest another function that maybe is even better than the above two.
I'd probably pick Jesse Farmer's algorithm as it's the simplest to understand. Plus, it works.
I guess the only other question would be: Is there some inherent weighting associated with the values that you'll be selecting?
That is, for each value in the 'selected values' array, is there some natural weighting -
like frequency as in words in the language, letters in text, numeric patterns in a series, etc.?
And if so, can this natural weighting be easily delineated and segmented in a 'weightings' array?
If the answer is in the affirmative, then I'd consider ordering the weightings array
based upon the discovered weightings and still use Farmer's algorithm -
with the discovered weightings as array element values.
There will be a list of names from my array, and each of the names inside the array would have different weight given to each name. However, some names may have the same weight. It is base on some result I have set.
Before assigning a weight to each of the name
I have to check something before assigning the weight to them. I will start off with a standard weight for each person say 0.8 for all people.
Next I will check whether they have perform a duty in the previous month. If so, I will -0.1 from people whom have perform the duty in the previous month. Next, I have to check whether is it a weekend. If so, I will -0.4 (excluding -0.1 previously). Thus, the chances to select this person for current month duty would be 0.3. Compare to a person whom had perform a duty the previous month and it is on a weekday, the chance would be like 0.55.
And thus, I would like to select the name for the next month duty based on the weight of each. Thus, after calculating for everyone.
PHP Code:
$names = array('Tom', 'Jerry', 'Joe', 'Nick', 'Paul'); // Eg of the weight for each of them. $weight = array('0.8', '0.55', '0.3', '0.55', '0.8');
And to say. Tom and Paul would certainly have a higher chance to being selected rather than Joe. Am I clear about this, or making you more confused of what I want.
@Infinitylimit: but that's very inefficient. I didn't have a solution but this had me thinking. I had an idea although it might not be truly random but you can generate random numbers between 0 and 1 and then add each one of those numbers to each weight respectively. Then simply take the element with the maximum weight. Now, logically, the one with the "heaviest" weight has higher chances of being the maximum.
// find the key to the maximum weight value after adding the random number $max = array_search(max($arr_weight_2), $arr_weight_2); echo $arr_val[$max]; ?>
HTH
Edit: After actually reading your post I think I might have jumped the shark. :\
__________________ PHPSimplicity
We don't need a reason to help people - Zidane [FF9]
@NVM
<fonz>Ahyee!</fonz>
Yeah I was trying to say by hack I meant it isn't the most effiecent but I knew it would work as intended.
However, so like how fast is array_search on large sets?
Last night I was thinking about this in a general form and I kept coming back to a matrix or result set for a weighted algorithm. By dropping 1 precision to 1-10 I think that would create a working constraint.
__________________ Hawk Enterprises -- Home to PHP games, open-source code, tutorials and free downloads
I'm in need of this particular function for my program. Weighted random function which basically pick out a random element inside a array base on the weight of the element.
This may or may not be what you're looking for. The code will probably need to be modified for your application, but this could give you a starting point:
This may or may not be what you're looking for. The code will probably need to be modified for your application, but this could give you a starting point: