Random Numbers, Counters and Drunk

From Learning

Jump to: navigation, search

So far, we have managed to generate some notes, adjust their pitch, volume and the rate at which they occur. But having a note occur every 500 milliseconds isn't very exciting. In this section, we will cover three options for generating melodies in Max:

  • random numbers
  • counters
  • an object called 'drunk'

We will use as a starting point the patcher that resulted from the Sliders and Dials tutorial, which is available at the bottom of that page.

Contents

Random Numbers

Max has an object for generating random numbers called 'random' (technically these are pseudo-random numbers - for more on generating random numbers see http://www.random.org/randomness/ ).

The 'random' object takes one argument (a number) generates a (pseudo) random number between 0 and one less than its argument. For example, create a 'random' object with an argument of 12, connecting a bang to its inlet and a number box to its outlet:

Randomobject.png


Hit the button several times and you will see several numbers generated in the range 0-11.

If we were to consider these numbers as MIDI pitches, they would all bit very low pitched notes. So it may help us to add another number to them - a constant - to make them higher in pitch. We can do this using the '+' object. By giving the '+' object an argument of 60, any number that is passed into its leftmost inlet will have 60 added to it. Place this '+' object, with an argument of 60, in between the 'random' object and the number box:

Plus 60.png

Hit the button several times again - you will see numbers in between 60 and 71 (this is 0-11 plus 60).


Next, lets connect these random numbers to the leftmost inlet (which controls pitch) of the 'makenote' object:

Random midi.png

(note that we have moved some other things around to make space in the patcher, this is quite normal when programming in Max).

Hit the button and generate some random notes. We now have a (very crude) melody generator.

Counters

Our random number generator is rather random. How would we generate musical sequences with a bit more order?

The 'counter' object allows us to count from one number to a higher number (or from a higher number to a lower number). Create a 'counter' with two arguments 0 (low number) and 11 (high number) and connect it to a number box. Connect a button to the inlet of the 'counter' object:

Counter.png


Hit the button a few times and you will see the number box value rise from 0 to 11 and then reset to 0.

In a similar way to the example with the random object, add 60 to the output value to place the numbers in a sensible range:

Counter plus 60.png


Now connect the output to the leftmost inlet of the 'makenote' object:

Counter midi.png


Hit the button connected to the counter object a few times and you will hear a rising chromatic scale.


'drunk'

Our counter object produces a very predictable melody. What would be better would be if we had a melody that was not random, but not entirely predictable either.

The 'drunk' object produces what is called a "random walk" (hence, 'drunk') - it will increase or decrease by a random amount, within a certain range. For example, if is currently at 60, we can specify that it may increase or decrease by a value of 3 from its current position. This means it will take a new value somewhere in the range of 57 to 63.

Create a 'drunk' object with two arguments - 127 and 3. The first value is the range - drunk will choose values between 0 and this number. The second value is the maximum amount the object can increase or decrease its value. Add a button and a number box to the drunk object as follows:

Drunk.png

Hit the button a few times and you will see the "random walk" produced by the drunk object as the numbers change in the number box.


Now connect the number box leftmost outlet to the leftmost inlet of the 'makenote' object:

Drunk midi.png


Hit the button connected to the 'drunk' object a few times and you will hear a melody generated by the "random walk".

The next tutorial, Storing and Recalling Information, will discuss how to make the "random walk" follow a minor scale, rather than choosing notes from a chromatic scale.

Next: Storing and Recalling Information

Full Max Patcher

Copy and paste this into an empty Max patcher:

----------begin_max5_patcher----------
925.3oc0YssbaBCD8Y6uBFdstYjD2r6a86HSlNXPwQMfjGrbaRyj+8BRBJwE
vKd.YmGLZP1fNmyt6QK32Vtvcq3E5AWmu4buyhEusbwB0TUSrvb9B273WRxh
On9Yt7i4aoEtqzeU4YhixLpT8kDyr6KnGnbYrjI3+nflH0KfWP3cnUN9DR0.
xbv4AyU8nfK4w4T0x78BVbV8pnWB4q6o5ajKiKcW43tMluys452GKSdhw20Z
E8CTqBwWMXNo8ZxRUqlX6O+JYsaKfbf8G0hgqvZCWY7Zphql68kKqNrBnzs8
nTJ3cKc3yIcapvtGIrSo6+DHnJClnF77qO1oxD4NUR.m96x6X8sSReQAG2zh
i7mKASji2vxyHyQfn.qUCQ9ClaDNlbCuoSX9hSH5BSXBTgznnonVaHczKTWV
gakI0qNFLFcjX6ZLOE1W6OQ0XFkAVMFY5pw.3POCds04Af7ZwysWaO0SIhib
IsvA4fwcqP9WrBU+AXASswS3fBEZLBUfMMdlPKDRDbKD7laAKjwaFX3HHy.7
5O6lA0QTHlA3nqjYPQLOUjWtR1NQ2T5GtdPYY12qLQjmSqBnmnK+hlIRXxW6
VVPmUV5xySy6PUSI5VS5k29yd5vUZCRcUfl8mojfL2ZPZKFAzh6wLQrD.Cwa
TLTW562GCwSlE2gLVZegydIyvko5Mh7U7.qGHa5lHMUoMQIDB0DmDEr5NMaC
mOxYyjkSoN8NzDVKmSkEBmhXIc3p4d5IFGp5I12ajOEQWhJQKmpigCtG+r6.
zmZsmUB6Iz1yjHY787ICQZu4lzSechYeLrwLSkjD0Wmre1aowDDwHH92gycv
TJ1sKiNxf44ZOwvMy.YXJ54Zy2K13a3tlLaZU6UcrSxfl62vk1GN.McOTEXA
vS63NXSWy9CUcsJZMoxPZ5Z8U5wPxielxERZUmCkfCMYhjtgsUP5bynSdlN3
vCJTWqWFZkHURxIb+4ZZuQ+Xp3AM7tTVqtYtYL9o+QKJ.UM+GkhChiEI0wv5
+YBm+goT5AIi2zS48MAkV+lmXooTdacImktWTV1XvfyCcFW.CoH.PhXYLEBA
SqsKlv2dgt.HxD1tXBAASA1ESDHXBYULgit4RmpdermERmf6YGSPRwOA3yNl
fXhisaJNl.LcBaOHAwv7DbO2XBT1jkMwA.I6tS2sWOJPZG.acDgAfH6UtAx7
1x6w4A.SV16FcyUtgCApRWZtT4Iuu7u.py6sOC
-----------end_max5_patcher-----------
  • This page was last modified on 8 December 2011, at 00:11.
  • This page has been accessed 992 times.