# Word Game: random % of when a letter should be displayed

This topic is 1512 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

The title may be a little confusing so I will try to the best of my ability to explain what I mean.

For games like scrabble/words with friends each match has a certain # of letters that can be used.

Now lets say for a game that has a endless amount of letters. I can't just choose them randomly 1-26 because the A and Z should be shown every 1 out of 26 times. This would cause most matches to end quicker then I would like because there are less "Real Words" with the letter A then with Z.

How would you create a random Letter being chosen or suggest to make it more fair?

I suppose I could make a little program to read every character from every word in a dictionary word list and find out the % of each character compared to the others, but I am sure there is an easier way?

##### Share on other sites

Wikipedia article on letter frequency. That has been known and studied for a very long time in different areas. There are plenty of tables with letter frequencies for different languages you can use. No need to make your own.

##### Share on other sites

"Now lets say for a game that has a endless amount of letters. I can't just choose them randomly 1-26 because the A and Z should be shown every 1 out of 26 times"

That's not how probability works. If you choose a random value between 1 and 26 several times you can get the same number 3 times in a row, one of the numbers on the 100th try only and another maybe 0 times since you have to stop at one time. That "1 out of 26" is true for one pick only, it's called an independant event, the fact that one number came out in a previous generation doesn't affect in any way the current one.

If you wanted to simulate that "1 our of 26 times" behaviour with an "endless amount of letters" you would actually need extra work, you'll need a list of already picked values and keep generating values until you get one that's not in the list.

Edited by DiegoSLTS

##### Share on other sites

As to the actual selection, what you describe sounds like weighted random selection. If I'm not much mistaken, it works something like this, in short:

• Each item (items in this case being letters) is assigned a weight, with higher weights corresponding to higher probabilities of being selected.
• Calculate the "total weight": the sum of the weights of the items.
• Whenever you want to select from the items, do the following:
• First generate a random value between 0 and the total weight
• Create a "running total", initialised to 0
• Finally, iterate over your items: Add the weight of the item to the running total; if this new running total value is higher than the random value, then select that item; otherwise, continue to the next item.
• (This can be thought of as laying the items end-to-end in a line, with their length corresponding to their weight. The random value then corresponds to a point made at some distance down this line, and the iterative process checks the end-point of each item to see whether it is beyond the random value, and thus that the point lies within the line-segment of that item (the preceding item, if any, having already been checked).)

##### Share on other sites

That's not how probability works. If you choose a random value between 1 and 26 several times you can get the same number 3 times in a row, one of the numbers on the 100th try only and another maybe 0 times since you have to stop at one time. That "1 out of 26" is true for one pick only, it's called an independant event, the fact that one number came out in a previous generation doesn't affect in any way the current one.

If you wanted to simulate that "1 our of 26 times" behaviour with an "endless amount of letters" you would actually need extra work, you'll need a list of already picked values and keep generating values until you get one that's not in the list.

Thankfully it's a bit easier (and more efficient) than that; you can take an array of possible values, Fisher-Yates shuffle it, and iterate along the array, reshuffling when needed.

For the OP: So if you want more of any particular letter, you can just add more of that letter to your initial array.  So you could use an alphabet like "AAAABCDDEEEEEEFGHHH..." to start out with, so that E is chosen twice as often as H and six times more often than B.  (Or whatever distribution you want.)

It might also be worth considering whether you want to prevent really bad draws (five Es in a row, seven consonants in a row, etc).  If so, then keep a list of recent letters like Diego says, and when the algorithm gives you a "bad" letter, just ignore it and ask for another.

##### Share on other sites

Thanks everyone. I wasn't expecting all these replies in such a short amount of time. I believe @Brother Bob hit it right on the money. The Letter frequency and the link works out even better since it has multiple languages.

##### Share on other sites

As a simple starting point, you might look at the Scrabble rules.

http://en.wikipedia.org/wiki/Scrabble_letter_distributions

There are 100 tiles, and 1 of them is Z, so Z has a 1% chance. But there are 12 E's, so there's a 12% chance for pulling an E.

- Eck

1. 1
2. 2
Rutin
21
3. 3
A4L
15
4. 4
5. 5

• 13
• 26
• 10
• 11
• 44
• ### Forum Statistics

• Total Topics
633742
• Total Posts
3013630
×

## Important Information

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!