Random Variation
Processing random( ) function
Using random values can add visual interest to artwork. The Processing random( )
function can be used in a variety of ways to create different types of variation.
Random Probability of Event Likelihoods
The Processing random function can be used to determine random probability of an event occurring, where the event is displaying some differential content each time an event happens. The event can be the frame-loop in draw, or it can be a user input such as mousePressed, or some combination events.
Random Event Likelihoods: In the following example, the random function returns a float value in the range between 0.0, 1.0. Using if / else conditional logic based on the value of the random value generated, the code below will print "option1" approximately 50% of the time.
Random Variation: Color variation
Although random can be used to select all components of a color, such as when using colorMode(RGB): Fully random color example:
Random variation: It's useful to use random( ) to generate a small variation in a HSB color by decomposing a given color into it's hue, saturation, brightness, and alpha components and then using random( ) to create slight variation in each color component value: example: color variation if the following code is executed in the draw loop
Select a random array element
When working with array index values, where the array index is an integer in the range of array index range: 0, myArray.length - 1 To select a random item from an array, the array index must be an integer and it can not exceed the max index: myArray.length-1. When using the random( ) function, it's important to understand that the returned random number is a float that is strictly less than the max parameter such that when it's truncated due to type-casting, the max value returned is the floor of the max parameter and this works well for array indexes
Last updated