Randomised Code


I am doing an EPQ on can code make music. And want to make the most effective yet best sounding randomised music I can. What would be the easiest way to go about doing this? I have already got chords and made randomised notes, but it’s not really independent enough for me. If anyone has any ideas, It would be greatly appreciated.



My projects are all in the realm of randomly generated algorithmic music, and I’m currently writing a theory for the next version of my latest algorithm. I can therefore provide some considerations for your project.

For this discussion, all music has properties in frequency space and time. Generally, good-sounding music has patterns in each, and is not totally random. Your use of chords is a pattern in space. Rhythm is a pattern in time.

My question is what you mean by “independence”. If by independence you mean that your music should sound like it’s played by different voices, then you should consider giving each voice a distinct frequency range, or at least avoid frequency overlap in the material they play. You could also vary the instruments you use.

If by independence you mean you want to do some work however!

Depending on how complex you want to make your program, you can consider incorporating into it space-time patterns commonly called motifs. My theory has as its building blocks various musical ideas, not least a generalisation of the motif (my technical name for it at the moment is a mouthful, polychronomorph, and the theory of its construction is chronomorphology).

I have the next week off and I may be able to produce both my theory and the next version of my algorithm by the end of it, depending on how many all-nighters I pull. If you’d like to wait to read and hear something, stay tuned! I also have some previous music produced by earlier iterations of my projects, which I can share if you’re interested, alongside the source code. At thousands of lines of code, it may not be the easiest way. But it does sound interesting.

If you’d like just the essentials, please fire away with whatever more specific questions you might have, and I as well as the kind people here will be happy to help.

Best of luck!

Supporting what @d0lfyn said, there is a balance to be struck between randomness (that would be super-independent) and familiarity. Too random, and it sounds like a chaotic mess (could be enjoyable - to some at least!). Too familiar, and it’s not interesting or outright plagiarism. A nice mix of familiar ideas and surprising twists - sounds cool :slight_smile:

Unless you have experience with music composition, it will be useful to no end for you to pick some songs that you like and look up their chords and/or notes and use them as a starting point. Or if you’re more confident with your musical skills, maybe still pick a genre to work in? I think that will give you the familiar footing and a starting point and allow you to start randomizing the elements and structure.

For example take the Infinite Bass Solo where DADABOTS trained a neural network to generate a bass solo from a couple of hours of bass-playing by Adam Neely. It’s incredibly familiar and constrained in one sense (“It’s just playing a bass solo!”) but if you listen to it then there is a richness of details in melody, rythm, emotion and articulation, etc. I think writing a Sonic Pi program to emulate what the neural network “randomly” does would be a huge challenge and would have to implement lots of details about bass players’ creative process.

So I’d suggest constraining yourself to your favorite genre/band, at least as a starting point.

Excellent suggestion! My music is experimental in nature and may not be what you’re looking for, unless you like pure theory and polyphony :wink:

It may be better to begin with something familiar!


Thanks for your replies! So basically for independence, I mean so it works out the stuff for itself while still using a set pattern, but a pattern that it makes up?

At the moment I’ve just got 4 jazz chords playing on a loop with a melody over the top, but all I have done is specify which a selection of notes that work with these chords, and then use choose to play which ever. But of course that isn’t truly random, and it’s computer random, so it is the same everytime, but the first time is random.

I also study computer science and music a level, so on both fronts I’m pretty good, it’s just trying to combine them together. It doesn’t need to be too complex or too over the top.

I am also definitely looking towards just jazz improv, played on a piano, so no band, to keep it simple.


Interesting, I’m not sure if you could use help with getting started or you good to go but would like pointers of what large-scale ideas to pursue?

So perhaps the choose part is what you might want to complicate a bit, first? The choose function is functionally stateless and picks any note, but when improvising you definitely base the note selection on whatever you had decided to play just before and/or where you want to get to eventually. If you have a live_loop then the last value of the last line (or the explicit return also I believe) will be available to the next iteration of the loop. So you can do something like:

initial_state = {
  last_note: nil,
possible_notes = scale(:c, :major)
live_loop :improv, init: initial_state do |state|
  # based on the current state, play the appropriate note(s)
  note = possible_notes.filter { |n| n != state[:last_note] }.choose
  play note
  # update state so that the what is played next would make sense following the above
  state[:last_note] = note
  # return the state for the next iteration

So this will not play the same note twice in a row - maybe a slight improvement but also hardly a rule you should always follow. But in this general approach of maintaining some state, you should be able to add different heuristics to follow (with appropriate randomness). Eg, maybe you count how many bars you’ve gone at the same tempo and use that to decide when to speed up or slow down? Or maybe pick some patterns you want to weave into the melody and prefer notes when you can match those patterns? Or see if a changing a scale would fit based on the last bar of notes you’ve played?

This is exactly what I needed! Yeah I kinda know what I want, but it’s just how to execute it, this is really helpful and the kinda thing I wanted to do… I’ll get started on this tomorrow and see how it goes.


If it helps, perhaps you could jot down a few bars of music, contemplate the rules you followed, and try to code those rules. That kind of helps me when I’m stuck.

If you want to make good music, start with the music.

1 Like

That’s a really good suggestion, thanks

1 Like

Does anyone know how to randomise the sleep time? While keeping it in a time that fits with the music. I have it basically quaver notes at the moment at 0.25s. Thanks

That rules out rand, which I’m sure you’ve considered. Perhaps you could give with_swing a try? I’ve never worked with time-warping before, but that seems like an interesting way to add time variation