问题描述
如何根据常用语音中的使用频率随机生成字母?
How can I randomly generate letters according to their frequency of use in common speech?
任何伪代码都值得赞赏,但在 Java 中的实现会很棒.否则,只要朝正确的方向戳一下就会有帮助.
Any pseudo-code appreciated, but an implementation in Java would be fantastic. Otherwise just a poke in the right direction would be helpful.
注意:我不需要生成使用频率 - 我相信我可以很容易地查找它.
Note: I don't need to generate the frequencies of usage - I'm sure I can look that up easily enough.
推荐答案
我假设您将频率存储为 0 到 1 之间的浮点数,总和为 1.
I am assuming that you store the frequencies as floating point numbers between 0 and 1 that total to make 1.
首先你应该准备一个累积频率表,即那个字母和它之前所有字母的频率之和.
First you should prepare a table of cumulative frequencies, i.e. the sum of the frequency of that letter and all letters before it.
为了简化,如果你从这个频率分布开始:
To simplify, if you start with this frequency distribution:
A 0.1
B 0.3
C 0.4
D 0.2
您的累积频率表将是:
A 0.1
B 0.4 (= 0.1 + 0.3)
C 0.8 (= 0.1 + 0.3 + 0.4)
D 1.0 (= 0.1 + 0.3 + 0.4 + 0.2)
现在生成一个介于 0 和 1 之间的随机数,并查看该数字在此列表中的位置.选择累积频率最小的字母大于随机数.一些例子:
Now generate a random number between 0 and 1 and see where in this list that number lies. Choose the letter that has the smallest cumulative frequency larger than your random number. Some examples:
假设您随机选择了 0.612.这介于 0.4 和 0.8 之间,即介于 B 和 C 之间,所以你会选择 C.
Say you randomly pick 0.612. This lies between 0.4 and 0.8, i.e. between B and C, so you'd choose C.
如果你的随机数是 0.039,那么它在 0.1 之前,即在 A 之前,所以选择 A.
If your random number was 0.039, that comes before 0.1, i.e. before A, so choose A.
我希望这是有道理的,否则随时要求澄清!
I hope that makes sense, otherwise feel free to ask for clarifications!
这篇关于根据使用频率随机生成字母?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,WP2