06.01.2013 Views

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

Learning Processing: A Beginner's Guide to Programming Images ...

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

56 <strong>Learning</strong> <strong>Processing</strong><br />

M e : Hey random, what’s going on? Hope you’re well. Listen, I was wondering, could you give me<br />

a random number between 1 and 100?<br />

Random : Like, no problem. How about the number 63?<br />

Me : Th at’s awesome, really great, thank you. OK, I’m off . Gotta draw a rectangle 63 pixels wide, OK?<br />

Now, how would this sequence look in our slightly more formal, <strong>Processing</strong> environment? Th e code below<br />

the part of “ me ” is played by the variable “ w ” .<br />

float w = random(1,100);<br />

rect(100,100,w,50);<br />

Th e random( ) function requires two arguments and returns a random fl oating point number ranging<br />

from the fi rst argument <strong>to</strong> the second. Th e second argument must be larger than the fi rst for it <strong>to</strong> work<br />

properly. Th e function random( ) also works with one argument by assuming a range between zero and<br />

that argument.<br />

In addition, random( ) only returns fl oating point numbers. Th is is why we declared “ w ” above as a fl oat.<br />

However, if you want a random integer, you can convert the result of the random function <strong>to</strong> an int .<br />

int w = int(random(1,100));<br />

rect(100,100,w,50);<br />

Notice the use of nested parentheses. Th is is a nice concept <strong>to</strong> get used <strong>to</strong> as it will be quite convenient <strong>to</strong><br />

call functions inside of functions as we go. Th e random( ) function returns a fl oat, which is then passed <strong>to</strong><br />

the int( ) function that converts it <strong>to</strong> an integer. If we wanted <strong>to</strong> go nuts nesting functions, we could even<br />

condense the above code in<strong>to</strong> one line:<br />

rect(100,100,int(random(1,100)),50);<br />

Incidentally, the process of converting one data type <strong>to</strong> another is referred <strong>to</strong> as “ casting. ” In Java (which<br />

<strong>Processing</strong> is based on) casting a fl oat <strong>to</strong> an integer can also be written this way:<br />

int w = (int) random(1,100);<br />

A random fl oat between 1 and 100.<br />

A random integer between 1 and 100.<br />

The result of random (1,100) is a fl oat. It can<br />

be converted <strong>to</strong> an integer by “casting.”<br />

OK, we are now ready <strong>to</strong> experiment with random( ) . Example 4-7 shows what happens if we take every<br />

variable associated with drawing the ellipse (fi ll, location, size) and assign it <strong>to</strong> a random number each<br />

cycle through draw( ) . Th e output is shown in Figure 4.8 .

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!