Random

One of the most powerful aspects of creating things through code is that of randomness. As humans, it can be pretty exhausting to come up with something different each time you are asked - a different drawing, a unique game-level map, or a new set of quiz questions, for example. Computers never tire of doing this, and it's pretty easy to tell them how. In Processing, you will use the random() function to have a new random number given to you. Here's a simple loop that draws a hundred white circles on a black background at random locations:


You'll notice that the random() function as used in the above sketch only uses one parameter - this specifies the upper limit of the number generated. Each time you call random() it will give you a float (not an int) number from zero up to (but not including) the upper limit. You can also use random() to give you a random color - here's the same code as above, but now each circle will be a different color:


Now, let's say you wanted to draw a random number of randomly colored circles. You could do something like this:


But - do you see any potential problems? Hint: when does n < random(100) in the for loop get evaluated? Right - it gets evaluated every single pass through the loop - so, it would be more efficient to do this:


This will only call random() once to determine the number of circles to draw (count). However, if you try to run this as-is you will get an error: cannot convert from float to int. This is because random() returns a float and the count variable is an int. It's easy to fix - just tell Processing to treat the float as an int like this:


Using random() to make decisions

Another nice thing you can do with the random() function is have it make decisions for you. This is especially helpful if you want to roll some virtual dice, or flip a virtual coin. To do this, you would call random() and use the result to determine which code to run. For example, if we randomly set the fill color before drawing our circle in the above sketch, it is like we are flipping 100 red/white coins:


Taking a more detailed look at this, **random(2)** will return a float ≥ 0 and < 2. Half of the time it will be less than 1. So, 50% of the time the if expression **random(2) < 1** will be true, and the fill color will be white. The other 50% of the time, the fill color will be red.

Pedantic random integers

Sometimes you'll want a random int instead of a float - like the number of times to draw a circle, as used above - and you may need to be careful how you convert from a float to an int. Simply casting it like we did above performs what is called a truncate - everything past the decimal point is just dropped. So even if random(100) returned 99.999, the count variable would be 99 (the mathematical term for this is floor, which is an actual function you can use in Processing). You might think that you could just round-off the random number, and that would work, but you would only get half as many 0's or 100's as the numbers 1 through 99. Only the values 0 up to .5 will round down to 0, but the values .5 up to 1.5 will round to 1. The easiest, but maybe a little counterintuitive solution is to just add one to the random range, and cast it to an int. So, if we want numbers from 0 through 100, we would use:


Anything greater than 100 will get truncated to 100. This technique will become especially useful when you start to use switch statements for choosing between more than two things.