lundi 25 janvier 2021

Select a string from list with a probablity

I was trying to create a function that returns a random word character (letter) with a given probability. My reference is this wikipedia page. In the table you can see the letter t has a 16% chance being the first letter. However, the letter x has 0.045% chance. I want to include every letter.

I'm sure that there is a better way than doing this:

import random

def selectLetter():
    letters = ['t', 't', (x16000 times), 'o', (x7600 times),... , 'x', (x45 times)]
    random.choice(letters)

Bonus points: if the code is capable of accepting a set/list (or whatever) of letters and excluding them and adjusting the probabilities accordingly. It's not necessary but it would be great!

How do I create such a function?




Aucun commentaire:

Enregistrer un commentaire