随机生成信根据其使用频率? [英] Randomly Generate Letters According to their Frequency of Use?

查看:142
本文介绍了随机生成信根据其使用频率?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怎么能随意根据普通话使用它们的频率产生信吗?

How can I randomly generate letters according to their frequency of use in common speech?

任何伪code AP preciated,但在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.

首先,你应该prepare累积频率表,也就是这封信的频率面前的总和,所有字母。

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。

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!

这篇关于随机生成信根据其使用频率?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆