Processing+-+Random+Numbers

Warm-up
Play the game: Poke the Penguin

How does the computer decide when the bunny will bite? Is it the same every time? It is a random number of pokes. It is not the same every time.

Random Number Generators
Any good computer game has some randomness in it.

What are some ways to get random numbers in real life?

A computer uses a Pseudo-Random Number Generator to generate a number between two numbers. Watch this video (also on the transfer drive):

media type="youtube" key="itaMNuWLzJo?feature=player_embedded" height="360" width="640"

Why are computer number generators called "Pseudo-Random"?

Remember: computers can only do what we tell them to. So how do we get computers to "make up" a random number?

What are some other ways that computers could make more random numbers?

Now, try modifying the seed on this program and check out the results (scroll down on the page to get to the program). There is a challenge in this program to modify the algorithm for making a random number. Can you make it better?

Random Numbers in Processing
To create a random number in Processing, we use one of these commands (a.k.a functions): code format="java" random(max); random(min, max) code These will create a random **float** number! The first one creates a random number from 0 to the max number (but doesn't include the max number!) and the second creates a random number between min and max (but not including max).

Try it! 1. Try typing in the following code (or use copy and paste); code format="java" float ranNum1; float ranNum2; void setup { ranNum1 = random(10); ranNum2 = random(50, 70); println("Number between 0 and 10 " + ranNum1); println("Number between 50 and 70 " + ranNum2); } void draw { } code Run your program three times to see what it does. Then, copy the code from the setup section to the draw section and run it again. You'll see it print out many many times as the draw function runs over and over and over and over...

2. If you want to use random to create int numbers, you'll need to convert your random numbers to ints. Here's an example of how to do that: code format="java" int ran = int(random(5)); //makes a random int between 0 //and 5 (but not including 5) code To turn a random number into an int, simply add int around the random call.

Add code to your program from ex. 1 to make your program print out random integers between 0 and 10 and 50 and 70.

3. Can you use this new knowledge to make a rectangle jump around the screen randomly? If it is going too fast, you can make it slow down using the frameRate(rate) command (ex. frameRate(1)) to Processing how often to redraw the screen (this will redraw 1 time per second). Hint: Think about how you change the position of a rectangle. What is the maximum value that you want the position to have? What is the minimum value?

4. Can you use this new knowledge to make a disco-dance party in the background? Again, you can play with the frameRate command if it is going too fast!