Tutorial 10: Random Drawing
Introduction
In this tutorial, we will further explore drawing using the lcd object. However, instead of explicitly drawing shapes, we will look at ways of automatically generating draw commands using the random and drunk objects.
Many different art forms use random and pseudo-random techniques for generating interesting or unexpected content. We will see how to use the random object to produce a range of numbers for source material, since this is the method used within Max to create random material. The drunk object provides a way to perform a random walk through a number range, which can provide the unexpectedness of random number generation while avoiding undesired large jumps in the number generated.
To open the tutorial patch please open 10mRandomDrawing.maxpat
from the the zip archive, which is available for download at the top of this page.
Drawing with random
Take a look at the tutorial patcher. At the top left of the patcher is a patch that uses the random object (colored green) to generate a random number. When we hit the button connected to it, the output of the object is a number between 0
and 999
– giving us a range of 1000
potential random numbers. That is the purpose of the object argument; it sets the range of numbers the object can generate.
Each time you click on the button, the random object generates another number. If you want to change the range of the output, you can change the argument – or you can send a new value into the right inlet of the random object. If we enter 20
into the connected number box at the top, we can see that hitting the button will now generate numbers between 0
and 19
. Now, let’s use this to generate some interesting graphics in the lcd object at the bottom-left of the patch.
The patcher object labeled example 1 is an automated graphics generator for the lcd below. A metro object provides a steady stream of bang
messages into five random objects, each with a range that is appropriate to the size of the lcd object (which is 320
pixels wide and 240
pixels tall). The output of the the first two random objects are manipulated with some math objects to create the numbers we need; everything is then packed together to create a list of seven numbers (via the pack object); the message paintoval
is placed before the list (via the prepend object), and then sent to the lcd.
In order to see what the seven numbers in the list represent, we should check the lcd reference text. We can do this by unlocking the patcher, selecting the lcd object, then choosing Reference from the Help menu. This displays the lcd manual page, where we can review the paintoval
message.
According to the reference manual, the arguments are left, top, right, bottom and color (sent as a list of three values representing the red, green, and blue amounts of the color to be used). Looking at the automated drawing code, we see that the first random object provides the left and right location (with 5 pixels added and subtracted to provide a 10 pixel-wide shape); the second random object is similar, but provides the top and bottom locations for the shape. Finally, the last three random objects create three random numbers (in the range of 0
– 255
) that will create a random RGB color for drawing.
When we turn on the metro with the toggle box, we see that the lcd quickly becomes filled with small (10 pixel) circles of random color. This is a great example of truly random location and color selection. But what if we want to exert a bit more control over the drawing location?
Drawing with drunk
There are many types of random behavior that can be modeled by the computer. The random object provides the closest approximation to "pure" randomness we can get; every number within its range has an equal probability of being selected. An alternative to the random object is the drunk object, so-called because it allows you to perform the “drunken walk” through a range of numbers; this randomization (technically a variant of something called brownian motion) characterizes many "random" processes in nature.
Just below our random example (at the top left of the patch) is a second test patch for the drunk object. If you click on the connected button, you will see that it gives us a random number based on the range provided by its first argument (an argument of 1000
gives us numbers between 0
and 999
). Hitting the button a second time, however, gives us a somewhat different result: we still get a random number, but it remains within the range of +/- 20
of the previous value. Continue hitting the button, and you will see the value will always stay within 20
steps of the previously chose number. The second argument to drunk gives us the maximum step size for each receipt of a bang
.
As with the random object, both arguments to drunk can be changed by connecting number boxes to the provided inlets. In the case of the drunk object, the second inlet changes the range value, while the right (third) inlet changes the step value. If we change the range and step sizes to smaller numbers (like 10
and 2
respectively), we can see how the output is clamped to a smaller range when we click the button again.
We can wipe the lcd canvas clean by clicking the message box labeled clear
just above the lcd in the tutorial patch. The patcher object labeled example 2 shows how we can use the drunk object to perform another automated drawing task. In appearance, it is very similar to the drawing routine that we used to demonstrate the random object, merely with one object substituted for another. After clearing the lcd object, start the metro by clicking the toggle above it. The lcd object is sent paintrect
messages with a list of generated integer values; the resulting rectangles are scattered around the lcd, but never make a large leap because they are constrained by the small step size (20
by 15
pixels) of the drunk objects. You will also note that the color changes very slowly, because the color arguments created are also constrained by the step size (5
) of the drunk objects.
More random drawing
The third set of automated drawing objects is in the patcher object labeled example 3. This section of the patch looks somewhat more complicated, and uses a new drawing message for the lcd object: framepoly
. If we reopen the lcd reference page, we see that this message expects a series of x/y pairs for creating a polygon frame, where each x/y pair represents a corner or vertex. Our automated routine uses eight random objects to create four pairs of coordinates, these are combined into a list (with pack), prepended with the framepoly
message (with prepend), then sent to the lcd. Rather than embed the framepoly message with color information, we separately calculate a random set of three values, make them into a list, and create an frgb
(for foreground RGB) message that is also sent to lcd.
This routine takes a different approach to the random point selection. Rather than randomly selecting any point on the lcd surface, the random numbers generate a value from 0
- 9
, then multiply the result to complete the coordinate selection. In the case of horizontal (x) coordinates, the value is multiplied by 32
; for vertical (y) coordinates, the value is multiplied by 24
. Thus, the lcd surface is divided into a 10x10 grid, and all coordinates are "locked" into this grid.
When we turn on the metro for this section of the patch (via the toggle object above it), we see that the polygon’s frame is drawn in the generated (random) color, but we also quickly see the result of limiting the random number selection: the graphic takes on a web-like quality, especially noticeable along the edges. By slightly changing the way we use the random object, we have implied some control on the generated result.
A slightly different result can be seen if we replace the random objects used for coordinate creation with drunk objects. Replace all of the random10
objects with drunk10 2
objects. Now, when you run the patch, the creation of the web seems even more controlled. You can further tweak the drawing function by replacing the color generators, changing all random255
objects to something like drunk255``15
. This will cause the color to change smoothly, giving you a different perspective on the generated drawing.
Summary
We’ve learned a little more about drawing in the lcd object, using the paintoval
(to create small circles), paintrect
(to create small squares) and framepoly
(to create jointed lines) messages. More interestingly, we’ve also learned to have Max perform the drawing for us, using the random and drunk objects to generate coordinates and colors on the lcd surface. We’ve also seen, using ranges and multiplication, that we can manipulate the random number generation to fit application-specific needs. It should be no surprise that the random and drunk objects are at the heart of many generative music and graphics applications.