Making random only appear once

PLEASE DO NOT RUN THIS CODE IF YOU HAVE EPILEPSY

So I was able to create randomly generated green colours for a golf course, however it is consistently running and I don’t know what kind of ‘if’ statement I should be putting to only make it run once.

NOTE: I am currently studying processing and am learning arrays in a lecture this week so if it’s that then I’ll just wait for the lecture XD

int size = 40;

void setup() {
  size(1280, 960);
}

void draw() { 
       background(255);
for (int row = 0; row < height; row++){
  for(int col = 0; col < 21.5; col++) {
    
  noStroke();
  fill(color(random(20), random(255), random(10)));
  rect(row*size, col*size, size, size);
  }
}

this is one of many ways to do it and the most simple

int size = 40;

void setup() {
  size(1280, 960);
//}

//void draw() { 
       background(255);
for (int row = 0; row < height; row++){
  for(int col = 0; col < 21.5; col++) {
    
  noStroke();
  fill(color(random(20), random(255), random(10)));
  rect(row*size, col*size, size, size);
  }
}
}

this is other way to delay the random

int size = 40;

void setup() {
  size(1280, 960);
frameRate(.11); // change this number to get variable delay  
}

void draw() { 
       background(255);
for (int row = 0; row < height; row++){
  for(int col = 0; col < 21.5; col++) {
    
  noStroke();
  fill(color(random(20), random(255), random(10)));
  rect(row*size, col*size, size, size);
  }
}
}

Hello yagirl,

All you need to do is to move everything inside your setup() function and delete the draw function. Setup will only run once at the begining of your program whereas draw will run continuously.

What you can do also is keep the current code that you have and use noLoop() at the end of your draw function.

Hello,

You should be looking at all the references for elements of your code.

  • setup()
  • draw()
  • random()
  • And the related references that are listed!

Lots of good resources (tutorials, examples and references) on the Processing website:
processing.org

Example:

float ran; //global scope

void setup()  //runs once
  {
  size(200, 200);
  ran = random(100);
  }

void draw() //60 times a second
  { 
  background(255);
  circle(width/2, height/2, ran);
  }
  
void mouseClicked() //Updates when mouse clicked
  {
  ran = random(100);
  }

:)

Yep first thing I looked at was the references on processing but it’s not a very extensive run through. I understqnd setup will run it once but now i’m having another problem where if I make a ball run through it, it leaves a trail. I can’t put a white background on it because that doesn’t work, does anyone know what else I can do?

float yPos;
float xPos;
float xBallPos;
float xHolePos;
float lastBallx;
float b;
float r;
int xDirection;
float ySpeed;
int x;
int y;
int size = 40;
boolean launch;
int n = 1;
color[] c;


void setup() {
  size(1280, 960);
  stroke(255);
  xPos = 50;
  xBallPos= 50;
  xDirection = 1;
  yPos = 940;
  ySpeed = 10.0;
  r = random(0, 1025);  //initial random goal position
  c = new color [n];
 Grass();
}


void drawBall() {
 fill(255);
 stroke(255);
 circle(xBallPos, yPos, 20);
 }
 
 
 // create a hole with random x position
 void drawHole() {
 fill(180);
 stroke(255);
 circle(xHolePos = r, 20, 25);
 }

void drawPlayer() {
 fill(255,249,32);
 stroke(0);
 circle(xPos, 910, 50);
 fill(0);
 circle(xPos-10,907,10);
 circle(xPos+10,907,10);
 arc(xPos, 915, 25, 25, 0, PI);
}

void drawSurprise() {
 fill(255,249,  32);
 stroke(0);
 circle(xPos, 910, 50);
 fill(0);
 circle(xPos-10,907,10);
 circle(xPos+10,907,10);
 circle(xPos, 920, 15);
 }
 
 void Grass(){
for (int row = 0; row < height; row++){
  for(int col = 0; col < 21.5; col++) {
    for(int i=0; i<n; i++){
   c[i] = color(random(0), random(180,255), random(0));
   
    noStroke();
    fill(c[i]);
  rect(row*size, col*size, size, size);
  }
}
}
}
 
 
 void keyPressed()
{
  if(key == 32)
      launch = true;
     }
     


void draw() { 
//make the playing field
fill(250, 237, 139);
 stroke(250, 237, 139);
 rect(0,860,1280,960);


  if(launch == true){
      drawSurprise();
      drawBall();
      drawHole();
      yPos = (yPos - 10);
      xBallPos = xPos;    
  }
  
     if(yPos < 6){
     launch = false;
     //Reset key stroke 
     key =1;
     //reset ball y pos
     yPos = 940;
     // create next random hole positioun
     r = random(0, 1026);
     }
  

  if (launch ==false){
    drawHole();
    
  drawPlayer();
     if (xPos > 1200) { 
         xDirection = 0; 
         }
     if (xPos < 50) { 
         xDirection = 1; 
         yPos = yPos + 1;
         }
         
//player movement    
    if (xDirection == 1){
         xPos = xPos + 10;}
     { 
      if (xDirection != 1) {
        xPos = xPos - 10;
     }
     
      }
      
  }
}