I need to generate a random letter, but with the likeliness of a letter being generated approximately the same as the ratio of tiles in Scrabble. So an E is 8 times more likely to occur than an X for example. My solution at the moment is to create an array with 8 'e's, 4 'm's, 1 'x' and so on, then to generate a random number equal to the number of items in the array and extract that letter.
Is there a better way?
Weighted Random Letters
Moderator: General Moderators
i did something simlar where random pictures were chosen to be displayed, but some pictures had more weighting, and were more likely to be displayed.
I went down the array route that you described, and it worked pretty well
I went down the array route that you described, and it worked pretty well
Code: Select all
// Array of priorities
// 1/2 chance of selecting an image rated as HI
// 1/3 chance of selecting an image rated as MED
// 1/6 chance of selecting an image rated as LOW
$priority=array(
"HI",
"HI",
"HI",
"MED",
"MED",
"LO"
);