# Fiction names generator using a Markov chain algorithm

I have created this name generator because I will need pilot names for the AI drivers of my drift racing game project! You can see it in action below and I will explain the basics of how the Markov chains work and how they are used here.

:embed https://microstudio.io/gilles/namegenerator/

According to Wikipedia, *A Markov chain is a stochastic model describing a sequence of possible events in which the probability of each event depends only on the state attained in the previous event*.

If you take a list of F1 driver names and run them through some analysis, you can compute a few probabilities:

- what is the probability that the name will be made of 10 letters? or 12? or 9?
- what is the probability that the name starts with the letter "A", or "B", or "C"...
- what is the probability that letter "E" will be immediately followed by letter "B", letter "D", letter "Z"?

Our Markov chain algorithm will do just that, it will count how many names have 10, 11, 12 letters (...), how many names start with "A", "B", "C" (...) and how many "A"s are immediately followed by letter "X", "Y", "Z" (...). Once the count is made, the algorithm knows the probability of any one of these "events". It is thus capable of generating a similar name, here is how it will proceed:

- pick the length of the name (how many letters), taking into account the probability of every possible length
- pick the first letter, taking into account the probability that it is "A", "B", "C" ...
- for current letter, pick the next letter, taking into account the probability that current letter is followed by any other letter ; and repeat

All these picks use a uniform random number generator (microScript's built-in `random.next()`

function). The random number is then projected onto the probability distribution of the desired event. This sounds complicated, but it isn't, you can think of it like this: if the probability of event 1 is 20% and the probability of event 2 is 80%, generate a random number between 0.0 and 1.0, if it is less than 0.2 then pick event 1, else pick event 2. That's exactly what the algorithm does, just with a larger number of events. Each event has its range within [0 .. 1], like [0 .. 0.2] and [0.2 .. 1.0] in our example.

Feel free to clone the project, you can easily inject your own list of reference names and see the results you will get!