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, click on the green Open Tutorial button in the upper right-hand corner of the documentation window.
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 and – giving us a range of 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 into the connected number box at the top, we can see that hitting the button will now generate numbers between and . 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 messages into five random objects, each with a range that is appropriate to the size of the lcd object (which is pixels wide and 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 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 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 – ) 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 drunkobject. 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 gives us numbers between and ). 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 +/- of the previous value. Continue hitting the button, and you will see the value will always stay within steps of the previously chose number. The second argument to drunk gives us the maximum step size for each receipt of a .
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 and 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 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 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 ( by 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 ( ) 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: . 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 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 (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 - , then multiply the result to complete the coordinate selection. In the case of horizontal (x) coordinates, the value is multiplied by ; for vertical (y) coordinates, the value is multiplied by . 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 random objects with drunk 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 random objects to something like drunk . 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 (to create small circles), (to create small squares) and (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.
See Also
Name | Description |
---|---|
random | Generate a random number |
drunk | Output random numbers within a step range |