Chapter 5: Musical Mathematics
5.1 Probability and Statistics in Music
Notes follow probabilities: tonic notes recur, chromatic tones appear less. Probability and statistics let us tally those choices, whether in a Bach chorale or a Coltrane solo. In Python we’ll call on random, NumPy, and Matplotlib to test musical data.
5.2 Generating Musical Sequences with Weighted Chance
Probability isn’t just analysis. It drives creativity. Assign a weight to each note, then let randomness pick pitches in proportion to those weights.
import random
random.seed(14) # pinned so you can verify; delete for a fresh melody
notes = ['C', 'D', 'E', 'F', 'G', 'A', 'B']
weights = [0.1, 0.15, 0.2, 0.15, 0.2, 0.1, 0.1] # likelihood for each note
random_sequence = random.choices(notes, weights, k=10)
random_sequenceVary the weights to tilt your melody toward major, minor, or modal flavors.
Continue reading this chapter
You're reading a free preview. Purchase the book to unlock every chapter in Artificial Intelligence for Musicians.
Want a hand with the code or building your own setup?
I'm an AI/ML engineer and a professional musician. Bring a question, a real project, a tool you wish existed, or a workflow that's eating your time, and we'll build it on your machine with your files. First call is free, 15 minutes, no commitment.
Book a free 15 minute call →