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

Started by
5 comments, last by Eck 9 years, 5 months ago

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?

Coding Samples :: Tips :: Discussions :: Game Directory => Game Makers Forums Online RPG Creator
Advertisement

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.

"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.

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).)

MWAHAHAHAHAHAHA!!!

My Twitter Account: @EbornIan

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.

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.

Coding Samples :: Tips :: Discussions :: Game Directory => Game Makers Forums Online RPG Creator

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

EckTech Games - Games and Unity Assets I'm working on
Still Flying - My GameDev journal
The Shilwulf Dynasty - Campaign notes for my Rogue Trader RPG

This topic is closed to new replies.

Advertisement