Processing: How to Randomize Textures


Scenes with custom textures always seem to turn out better for me, but they’re hellish to make. I’ve spent hours in GIMP hand-drawing patterns, carefully testing out layers to get  effects. It’s a hassle.

However, I’m learning to automate more of that sort of thing with Processing. What’s really nice about that is you can get a texture that changes subtly every time you run the script if you use the Random function to assign coordinates, weights, and transparency values.

Some important functions to note before you get started:

Conjure a Random Float
random(endNum, startNum);

Draw a Line
line(x1, y1, x2, y2);

Designate the Width of the Line

Designate the Color of a Shape
fill(redValue, greenValue, blueValue, alpha);
*RGB values are from 0 to 255
*Alpha is the transparency value

The meat of the script is a For Loop filled with variables that are set to random values. My image is 1000 pixels by 1000 pixels, so I’m randomizing the x and y coordinates to a maximum of 1000. If you keep the y value constant when making a line, you get a horizontal one. Keeping the x value constant makes one straight up and down.

for ( int i=0; i<150; i++ ) {  Setting this to 150 will create 149 sets of lines
float varx = random(1000);
float vary = random(1000);
float alpha = random(100);
float weight = random(5);  
strokeWeight(weight);  Make the line between 0 and 5 pixels wide
stroke(34, 139, 34, alpha);  Customize to any RGB color code
line(0, vary, 1000, vary);  Draw a horizontal line at the random y value
line(varx, 0, varx, 1000); Draw a vertical line at the random x value

That’ll get you somewhere near this:


If you don’t like a particular configuration, you can play with the variable parameters or just run the script again. I also threw in another For Loop to generate patchy rectangles. That can be saved to a png for easy importing into a Blender scene. The full script is below.

The Code

void setup(){
size(1000, 1000);
for (int i=0; i<=150;i++){
float varx = random(1000);
float vary = random(1000);
float alpha = random(100);
float weight = random(5);
stroke(34, 139, 34, alpha);
line(0, vary, 1000, vary);
line(varx, 0, varx, 1000);

for (int x=0; x<=30; x++){
float alpha = random(100);
fill(34, 139, 34, alpha);
float varx = random(1000);
float vary = random(1000);
float rwidth = random(300);
float rheight = random(300);
rect(varx, vary, rwidth, rheight);



7 Replies to “Processing: How to Randomize Textures”

    1. I have a theory that there aren’t many Processing blogs out there because The Coding Train already covers everything. And no one can teach like Daniel Shiffman. That guy’s got the energy of a hoard of meerkats.


      1. for me it processing is unknown, and yes, after some years of coding in processing you too have ideas brimming in the head

        and yes tag your posts

        on wordpress only me do it as of now

        Liked by 1 person

Leave a Reply to ARJ Cancel reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: