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.
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
source to share
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.
source to share
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.
source to share