生成随机字母表,其中一些字母表出现频率较高(vc++6.0)

发布于 2024-08-06 00:50:13 字数 565 浏览 7 评论 0原文

基本上我有一个生成随机字母的函数。我已经使用 rand() 函数生成数字,并将它们转换为相应的 ascii 等效项。 但我希望与其他字母相比生成更多的元音。 如果生成了 10 个字母,那么应该有 2 个 o、3 个 a 等。 我该如何做到这一点?在vc++6.0中。

编辑: 实际上我正在用 vc++6.0 制作拼字游戏作为我的大学项目。所以在我的板下我有 7 个按钮,每个按钮上我显示一个随机字母。所以我想要的是......就像在拼字游戏中我们有:

 1 point:  E ×12, A ×9, I ×9, O ×8, N ×6, R ×6, T ×6, L ×4, S ×4, U ×4
 2 points: D ×4, G ×3
 3 points: B ×2, C ×2, M ×2, P ×2
 4 points: F ×2, H ×2, V ×2, W ×2, Y ×2
 5 points: K ×1
 8 points: J ×1, X ×1
10 points: Q ×1, Z ×1

所以就像你从上面描述的集合中选择 7 个随机字母一样,我想要以同样的方式生成 7 个字母。

basically i have a function that generates random alphabets .i have done this using the rand() function to generate numbers and them converted them to their corresponding ascii equivalents.
but i want the vowels to be generated in higher numbers as compared to other alphabets.i.e
if have generated say 10 alphabets then there should be like 2 o's,3 a's etc.
how do i do this??in vc++6.0.

Edit:
actually i am making scrabble as my college project in vc++6.0. so under my board i have 7 buttons on which i am displaying a random letter on each.so what i want is that ..like in scrabble we have:

 1 point:  E ×12, A ×9, I ×9, O ×8, N ×6, R ×6, T ×6, L ×4, S ×4, U ×4
 2 points: D ×4, G ×3
 3 points: B ×2, C ×2, M ×2, P ×2
 4 points: F ×2, H ×2, V ×2, W ×2, Y ×2
 5 points: K ×1
 8 points: J ×1, X ×1
10 points: Q ×1, Z ×1

so just like you would pick 7 random letters from the above described set, i want the 7 letters to be generatd in the same way.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

栖竹 2024-08-13 00:50:13

您可以添加另一个 rand 函数。
Fe(抱歉,这只是伪代码)

if(rand(0,10) >= 5) {
    //generate here a vowel at random
} else {
    //generate a normal letter or a random letter (including vowel)
}

这将以 50/50 的几率生成元音,您可以通过更改 5.

Bobby来更改此几率

You could add another rand function.
F.e. (sorry, this is just pseudo code)

if(rand(0,10) >= 5) {
    //generate here a vowel at random
} else {
    //generate a normal letter or a random letter (including vowel)
}

This will generate vowels at a 50/50 chance, you can change this chance by altering the 5.

Bobby

苍景流年 2024-08-13 00:50:13

给每个值一个权重。例如a = 5,b = 5,c = 2,... q = 1,z = 2等

权重越高,您越希望字母出现

计算随机字母,将所有权重加在一起并在 0 和总重量之间随机选择一个。

然后选择与抽出的数字对应的字母:

如示例所示:
如果你画0-4,你就拿a,如果你画5-9,你就拿b,10-11 =c等等

give every value a weight. E.g. a=5, b=5, c=2, .... q=1, z=2 etc

the higher the weight, the more often you want the letter to appear

to calc a random letter, add all the weights together and pick a random between 0 and the totalweight.

then select the letter which corresponds with the drawn number:

as in the example:
if you drew 0-4 you take the a, if you drew 5-9 you take b, 10-11 =c etc etc

深海蓝天 2024-08-13 00:50:13

一个简单的解决方案是使用 2 个列表。一种带有元音,一种带有辅音。
现在通过选择 N 个随机元音和 M 个随机辅音来构建新的字母表,其中 N > 。 M 和 N+M = 字母表的最大大小。

A trivial solution is to use 2 lists. One with the vowels and one with consonants.
Now construct your new alphabet by selecting N random vowels and M random consonants where N > M and N+M = max size of alphabet.

一个人的旅程 2024-08-13 00:50:13

我将用伪代码编写它。假设您已经定义了字母频率:

freq['a'] = 0.2
freq['b'] = 0.01
...
freq['z'] = 0.02

所有元素的总和显然应该为 1。

然后您可以定义一个带有间隔的数组:

intr['a'] = [0; 0.2)
intr['b'] = [0.2; 0.01)
...
intr['z'] = [0.98; 1)

然后当您在间隔 [0; 中生成一个随机数 n 时, 1),你只需要遍历区间数组并找到对应的字母:

for(letter = 'a' .. 'z')
  if n in intr[letter] then return letter;

为了提高速度,区间数组也可以使用整数来实现。

I'll write it in pseudo-code. Let's say you have your letter frequencies defined:

freq['a'] = 0.2
freq['b'] = 0.01
...
freq['z'] = 0.02

Sum of all elements should obviously be 1.

Then you can define an array with intervals:

intr['a'] = [0; 0.2)
intr['b'] = [0.2; 0.01)
...
intr['z'] = [0.98; 1)

Then when you generate a random number n in interval [0; 1), you just need to run through interval array and find which letter corresponds to that:

for(letter = 'a' .. 'z')
  if n in intr[letter] then return letter;

The interval array can also be implemented using integer numbers for speed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文