Page 1 of 1
Weighted Random Letters
Posted: Mon May 09, 2005 5:18 am
by onion2k
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?
Posted: Mon May 09, 2005 5:20 am
by JayBird
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
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"
);
Posted: Mon May 09, 2005 5:06 pm
by McGruff
Another way might be to reserve integer ranges: 0-7 for e's, 8-11 for m's, and so on. Set a $max value for the random number generator at the end of all that.