How to generate a random string of which the probability of each letter to appear is given?
My attempt, as shown below, generate a random string with equal probability of appearance for each letter. (I didn't use the whole alphabet)
The propriety frequency represent of the probability of that letter to appear in percentage. The sum of all frequencies equals to 100. The values of these frequencies has been generated dynamically. They are not static.
var array = [{value: 'A', frequency:15}, {value: 'B', frequency:5},{value: 'C', frequency:10},{value: 'D', frequency:20},{value: 'E', frequency:35},{value: 'F', frequency:15}]
function getRandomName (howmany) {
if(!howmany) {
howmany= 6;
}
var result = []
for(var n = 0; n < howmany; n++) {
var rand = array[Math.floor(Math.random() * array.length)];
result.push(rand.value)
}
var result = result.join('')
console.log(result.toLowerCase())
}