CS1335
  • Introduction
  • Assignments
    • A1: Meta, Metta
    • A2: Functions, Emotions
    • A3: Repeat Patterns
    • A4 - Objects: Things and No Thing
    • Final Inspirations
    • A?: Grid Patterns
    • Inspiration
  • Getting Started
    • Processing
    • PDE - Code Editor
    • Learning Processing
  • Java Language
    • Java Syntax
      • Typed-Variables
      • Float - Integer Conversion Errors
      • Modulus
      • Functions
      • Object Reference Data Types
      • Arrays
        • Class Example Code
      • Switch-Case Statement
      • Ternary Operator
      • Class
  • Project 1
    • Random Variation
    • Noise
    • HSB Color Mode
      • HSB Color Wheel
        • Dynamic ColorWheel
        • HSB Color Palette Tool
    • PShape Objects
      • Example Code: PShape
        • Inspiration
    • Modeling Emotions
      • Emotions
        • Kandinsky Color - Emotion
    • PShape with Contour
    • Recursion
      • Recursion Call-Stack
      • Recursion Examples
        • Example Code 1
        • Example Code 2
    • Recursion - PShape
    • Recursive Patterns
    • Planning Structure: Functions:
      • Example Code - Feb 19
      • Final Code Structure
    • Project 1: Programmatic Variations in Color
      • LerpColor
      • Map Function
      • Map with LerpColor
      • noise( )
    • Transforms for Mirroring
    • Project 1-Steps
  • Grid Based Designs
    • Computational Design
    • Artist: Victor Vasarely
    • Grid Pattern Design
    • 1D - Array of PShapes for Grid Layout
      • Truchet Tiling
      • Example Code S2020
      • Example Code March 11
      • Example - March9
      • Example Code
    • PShapes in Grid Regions
    • Grid Region Logic
    • Pattern Preview - Transforms: Translate & Scale
  • Project 2
    • Project 2 - 2D Arrays for Gradient Logic
      • 2D Array Grid with Labels
    • Grid Patterns using 2D Array Indexes: i, j
      • Example Class Code
    • lerpColor( ) and map( ) Functions
    • Demo Lerp Colors
    • 2D Arrays with lerpColor
    • Create PShape 2D Array
    • Function: Populate2DArray( )
    • Function: DisplayShapeMatrix()
    • Transforms for Position, Rotation, Scale of ShapeMatrix Elements
    • Project 2 - Steps
    • Animation for ShapeMatrix
      • Animation w/Noise
  • Object Oriented Programming
    • Introduction to Objects
    • Button States
    • Buttons as Objects
      • Button Class
    • Create Object Instances
    • Button Types
    • Modeling Buttons: States and Events
    • OOP - Inheritance
    • OOP - Polymorphism
    • Child-Class: PImageButton
    • PShape - SVG Objects
    • Menu of Buttons
    • ButtonGroup - Final Version
    • Slider Controller
    • UML Class Diagram
  • Project 3
    • Project 3 - Logic, Steps
    • Project 3 - Class Definitions
      • Button
      • PImageButton
      • ButtonGroup
      • Pattern
        • PShapes - SVG, Vertex Shapes
        • Setting Colors For Patterns
        • Pattern - With Child-PShapes
      • Slider
      • Particles
  • Modeling
    • UML Class Diagram
  • Resources and References
    • Glossary
    • Resources
      • Acoustics
      • Learning Science
        • Emotional Intelligence
      • Creativity
      • Conceptual Art
      • Books
        • Art
      • Games, Rules
      • Complexity
    • Random Inspiration
      • Ulm School
      • Heart-Mind, Mind, Body
      • Statistical Uncertainty
Powered by GitBook
On this page
  • Random Probability of Event Likelihoods
  • Random Variation: Color variation
  • Select a random array element

Was this helpful?

  1. Project 1

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.

//returns a float value between min, max, does not include the max value
//function signatures - 2 overloaded versions

float random( float max ) // 0 assumed to be the minimum value
float random( float min, float max )

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 chance example
float randChance = random( 0.0, 1.0);   //will return values between 0.0, .9999
if( randChance < 0.5) { //select midpoint of random range 
println( "option1"); //printed ~50% of the time
//put other code here if you want it exectued %50 of the time
}else{
println( "option2"); //printed ~50% of the time
//put other code here if you want it exectued %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:

fill( random( 255,), random( 255), random(255)); //random color

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

color c1 = color( 270, 100, 100, 100); //HSBA
float hueVariant = hue( c1 ) + random( -10, 10); // 20 degrees of variation
float satVariant = saturation( c1 ) - random( 0, 30 ); // subtraction gives 30 percent variation in saturation

fill( hueVariant, satVariant, bright( c1 ) , alpha( c1) );//color variation
rect( mouseX, mouseY, 100, 100); // drawn at mouse position

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

int[] myArray = new int[10]; //array with 10 elements, index 0-9
int length = myArray.length; //array object has length property
for( int i=0; i< length; i++){
    myArray[i] = i; //fill array element with the array index value
}
int randomIndex = (int) random( 0, length);  //returns values 0 - 9
println( myArray[ randomIndex ] );//will print an int: 0 - 9
PreviousClassNextNoise

Last updated 4 years ago

Was this helpful?