# Markov chains for beginners

**URL:** https://in-thread.sonic-pi.net/t/markov-chains-for-beginners/5304
**Category:** Creations & Ideas
**Created:** [March 25, 2021, 11:47am UTC](https://in-thread.sonic-pi.net/t/markov-chains-for-beginners/5304 "2021-03-25T11:47:20Z")
**Posts on this page:** 1
**Showing post:** 3

<div class="post-metadata">

### Author: ![Nechoj](https://dub1.discourse-cdn.com/flex017/user_avatar/in-thread.sonic-pi.net/nechoj/32/1814_2.png) [@Nechoj](https://in-thread.sonic-pi.net/u/Nechoj)
#### Post date: [March 25, 2021, 4:29pm UTC](https://in-thread.sonic-pi.net/t/markov-chains-for-beginners/5304/3 "2021-03-25T16:29:39Z")

</div>

Hi @amiika thanks for the support! Yes, in practise you would use hash maps for an efficient implementation of the Markov matrix, as you suggested. I tried to be close to the notion of a matrix because this is what you’ll find in the literature or in Wikipedia on the subject matter.

In the example [Harmonic random walk in major](https://in-thread.sonic-pi.net/t/harmonic-random-walk-in-major/5281) I am using a 2-dimensional hash map and a state space that is not just a scale of 7 notes but a complete selection of chords and tonic keys. But the idea is the same: Whenever you want to combine rules and randomness, Markov chains are a great way of achieving it.

Why would we want to combine rules and randomness? Well, the rules can implement the concept of _style_, while the randomness is the _variation_ within a style. If we would be able to map Beethoven’s style of composition inta a Markovian matrix and do the same with Schubert’s or Chopin’s style, they would have different Markovian matrices. And when you let the program play, it will produce a random output but sound like the respective styles (ok, in practise this would be a very difficult and complex task, but I just wanted to illustrate the idea of combining rules and randomness).

---

_[View the full topic](https://in-thread.sonic-pi.net/t/markov-chains-for-beginners/5304)._
