Random Music with Mathematica

Here’s some mathematica code that will generate random musical tunes. Sometimes they come out half decent… sometimes.


    (* randomwalk: generates a sequence where each element is within 2*range of the
       previous one. *)
    randomwalk[length_, range_] := Module[{ret={0}, cur=0},
        Do[AppendTo[ret, cur += Floor[Random[]*(2r+1)]-r], {n}];

    (* scaletone: index into a scale (of cents), incorporating negatives *)
    scaletone[scale_, index_] :=
        scale[[ Mod[index, Length[scale]] + 1 ]]
            + 1200*Floor[index/Length[scale]];

    (* palendromify: append the reverse of a list to itself *)
    palendromify[list_] := Join[list, Reverse[list]];

    (* randomtune: generate a nice random song *)
    randomtune[scale_, range_, notes_, length_] :=
        Scale[scaletone[scale, palendromify[randomwalk[notes, range]]]], 440, length];

    (* example *)
    Show[randomtune[JustMajor, 3, 32, 10]]

Try it out. Or if you don’t have mathematica, then… don’t try it out.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s