Need help making my code display random set of images

I am trying to make a code that will display random images,
I wanted to create a RandomCatGenerator that would have a random value between 1 and 3, and a random picture of a cat would show up depending on which number RandomCatGenerator would land on, this for some reason isn’t working and I think it has something to do with how I wrote my while loops. Let me know if this isn’t clear enough and if you need any more info on the project for helping. Thanks!

here is my code:

PImage cat1; //regular Cat
PImage cat2; //angry Cat
PImage cat3; //derpy Cat

float CatGenerator = random(1,3);

void setup() {
size(32,48);

while(CatGenerator = 1) {
image(cat1,0,0);
}

while(CatGenerator = 2) {
image(cat2,0,0);
}

while(CatGenerator = 3) {
image(cat3,0,0);
}

cat1 = loadImage(“regularcat.png”);
cat2 = loadImage(“angrycat.png”);
cat3 = loadImage(“derpycat.png”);

background(0);
CatGenerator();
}

The main problem here is that your while loops have no end condition.
The while loop will continue to loop while the statement is true;
For instance, let say that your CatGenerator is randomly initialised with the value of 2. The first while loop won’t execute because the condition isn’t true, then the second while loop will execute, saying that an image of a cat should be displayed at the top left corner. But because after that, the condition is still true, the loop will continue.

In fact, you shouldn’t be using while loops here, but ‘if’ statement. By the way, variable = value will assign the value to your variable, while variable == value will return the boolean corresponding to true if variable has this value, false if it doesn’t.

Also, one thing you should know is that what ask processing to be displayed is only displayed at the end of the draw() loop, so if you don’t have any nothing will be displayed.

One last thing, you are loading the images after telling processing to display them.

Here is something that should work

PImage cat1; //regular Cat
PImage cat2; //angry Cat
PImage cat3; //derpy Cat

float CatGenerator = random(1,3);

void setup() {
  size(32,48);
  
  cat1 = loadImage("regularcat.png");
  cat2 = loadImage("angrycat.png");
  cat3 = loadImage("derpycat.png");
  
  
  //CatGenerator();    //CatGenerator isn't a function, so calling it will not help
                       //you have init it randomly already
}

void draw(){
  background(0);     //the background should be reseted before drawing the image
  if(CatGenerator == 1) {
    image(cat1,0,0);
  }
  else if(CatGenerator == 2) {   //I added the else statements, but it is really not needed, you can stick with if statements only
    image(cat2,0,0);              //it is just to make less comparisons, because if the first condition is met, the others can't be true (same with the 2nd)
  }                               //alternatively, you could use switch() but no need to over complicate.
  else {
    image(cat3,0,0);
  }

  //once the program has reached this portion of the code, it will draw everything and start the draw loop again.
  //to avoid this never ending draw loop as you only need things to be drawned once, you can add noLoop(); in the setup function for instance.
}

if you want to have a new random picture each frame, you should add CatGenerator = random(1, 3); in the draw loop. Btw this is the reason I let cat1, cat2 and cat3 because if the program should only display one picture at each execution, the code can be shorten to that :

PImage cat;  //you only need to load one of the three pictures


float CatGenerator = random(1,3);

void setup() {
  size(32,48);
  if(CatGenerator == 1) {
    cat = loadImage("regularcat.png");
  }
  else if(CatGenerator == 2) {
    cat = loadImage("angrycat.png");
  }
  else cat = loadImage("derpycat.png");
  
  noLoop();
}

void draw(){
  background(0);     //the background should be reseted before drawing the image
  image(cat, 0, 0);
}

I hope that helps!

Thankyou! you definitely helped with explaining the loops, I copied the code and for some reason it still doesn’t work for some reason, it just keeps showing the derpy cat!

2/ I’m thinking it is because the float gives it decimal points, so the random # ends up having a decimal point like 1.5 and just going to the else which is derpy cat. How can I make the random only go to whole numbers?

So I’ve found that it was the problem! i just had to convert the float to int and it was good to go! here’s the code now:

PImage cat; //you only need to load one of the three pictures

//float CatGenerator = random(1,3);
float CatGenerator = random(1,3);
int newCatGenerator = int(CatGenerator);

void setup() {
size(32,48);
if(newCatGenerator == 1) {
cat = loadImage(“regularcat.png”);
}
else if(newCatGenerator == 2) {
cat = loadImage(“angrycat.png”);
}
else cat = loadImage(“derpycat.png”);

noLoop();
}

void draw(){
background(255); //the background should be reseted before drawing the image
image(cat, 0, 0);
}

Lots of resources here:

Example:

void setup() 
  {
  frameRate(1);
  }

void draw() 
  {
  float fRan = random(0, 5);
  int iRan = int(fRan);
  println(fRan, iRan);
  }

:)

1 Like

Oh yeah sorry I didn’t see that! You’re totally right though, the float is the problem (the probability that it returns exactly 1 or 2 is zero, therefore the last else statement is always true)

But you must be aware that random(a, b) (https://processing.org/reference/random_.html) will return a random number between a and b, but not including b.
Therefore, if you use int(), you will only have values in {1, 2} because the way int() works on positive number is the same as floor() (https://processing.org/reference/floor_.html). To have values in {1, 2, 3}, you either want to call random(1, 4) and then converting it with int(), or use the round() function https://processing.org/reference/round_.html

Hi

General idea close from your demand