0

Using propability in random word generator

Hey, I want to create a random word generator in Python and letters should be picked with certain probabilities (eg. A=3%, B=1%, ...). My first thought was a dictionary {'a':3, .. }, but I am sure there are better ways.. So can you tell me, if you could think of better data structures to implement a probability pick out of the alphabet? (set, list, heap, ... )

28th Mar 2020, 9:47 AM
Ditus
Ditus - avatar
4 Answers
+ 3
The usual approach is a probability density function (like the one from statistics class). In code that means you need just an array of probabilities [0.03, 0.01, ...]. A dictionary is fine too but a heap would be the wrong choice because all you need to do is run through the list. Then everytime you want to generate a letter you pick a random number `x ∈ [0; 1[` and initialize a counter to 0. You keep running through your array and add the probabilities to your counter, until `counter ≄ x`. And that's the letter you choose. I'm sure numpy or scipy have that built in too but I wouldn't know anything about that.
28th Mar 2020, 9:59 AM
Schindlabua
Schindlabua - avatar
+ 1
Schindlabua ahhh I could have come up with this one. That's the missing piece :)) thank you.. I will try it, but... If anyone knows the library, tell me please!
28th Mar 2020, 10:03 AM
Ditus
Ditus - avatar
+ 1
Immer gern :)
28th Mar 2020, 10:03 AM
Schindlabua
Schindlabua - avatar
0
Jay Matthews basically the same as my suggestion. Problem is: how do you pick with the entered probabilities? Random guess of a letter x and then keep it with the probability P(x), or else pick a new random one? I think that is no beautiful solution.
28th Mar 2020, 10:00 AM
Ditus
Ditus - avatar