Generation of random alphabets with some alphabets having a higher frequency of occurrence (vC ++ 6.0)

Basically I have a function that generates random alphabets. I did it using the rand () function to generate numbers and they convert them to their corresponding ascii equivalents. but I want the vowels to be generated in higher numbers compared to other alphabets.ie if they pronounced say 10 alphabets then should be like 2 o, 3 a, etc. how to do it in vC ++ 6.0.

Edit: I'm actually doing scrabble as my college project in vC ++ 6.0. so under my board I have 7 buttons on which I show a random letter on each. So what I want is .. as 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 how would you choose 7 random letters from the above set, I want the 7 letters to be generated in the same way.

+2


source to share


4 answers


You can add another rand function. FE (sorry, this is just pseudocode)

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 with a 50/50 chance, you can change this chance by changing 5.

Bobby

+1


source


give each value a weight. For example. 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 calculate a random letter, add all weights together and choose a random value between 0 and the total weight.



then select the letter corresponding to the reverse number:

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

0


source


The trivial solution is to use 2 lists. One with vowels and one with consonants.
Now create a new alphabet by choosing N random vowels and M random consonants, where N> M and N + M = the maximum size of the alphabet.

0


source


I will write it in pseudocode. Let's say you have certain frequencies of your letters:

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

      

The sum of all elements must be equal to 1.

Then you can define an array at 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 the interval [0; 1)

, you just need to start the interval and find which letter matches that:

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

      

Interval array can also be implemented using integer numbers for speed.

0


source







All Articles