# Need help making my code display random set of images

**URL:** <https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316>\
**Category:** Coding Questions\
**Created:** [September 19, 2021, 4:28pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316 "2021-09-19T16:28:13Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![colehodl](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@colehodl](https://discourse.processing.org/u/colehodl)\
**Post date:** [September 19, 2021, 4:28pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/1 "2021-09-19T16:28:13Z")

</div>

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();  
}

---

<div class="post-metadata">

**Author:** ![gitanerie](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@gitanerie](https://discourse.processing.org/u/gitanerie)\
**Post date:** [September 19, 2021, 6:15pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/2 "2021-09-19T18:15:59Z")

</div>

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

```auto
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 :

```auto
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!

---

<div class="post-metadata">

**Author:** ![colehodl](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@colehodl](https://discourse.processing.org/u/colehodl)\
**Post date:** [September 19, 2021, 6:58pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/3 "2021-09-19T18:58:35Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![colehodl](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@colehodl](https://discourse.processing.org/u/colehodl)\
**Post date:** [September 19, 2021, 7:04pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/4 "2021-09-19T19:04:04Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![colehodl](https://avatars.discourse-cdn.com/v4/letter/c/7c8e57/32.png) [@colehodl](https://discourse.processing.org/u/colehodl)\
**Post date:** [September 19, 2021, 7:18pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/5 "2021-09-19T19:18:19Z")

</div>

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);  
}

---

<div class="post-metadata">

**Author:** ![glv](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/glv/32/18785_2.png) [@glv](https://discourse.processing.org/u/glv)\
**Post date:** [September 19, 2021, 7:18pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/6 "2021-09-19T19:18:27Z")

</div>

> [@colehodl](#):
>
> How can I make the random only go to whole numbers?

> **[int() / Reference](https://processing.org/reference/intconvert_.html)**
>
> Converts any value of a primitive data type (boolean, byte, char, color, float, int, or long) or String to its integer representation. Whe…

Lots of resources here:

> **[Welcome to Processing!](https://processing.org/)**
>
> Processing is a flexible software sketchbook and a language for learning how to code. Since 2001, Processing has promoted software literacy within the visual arts and visual literacy within technology…

Example:

```auto
void setup() 
  {
  frameRate(1);
  }

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

```

`:)`

---

<div class="post-metadata">

**Author:** ![gitanerie](https://avatars.discourse-cdn.com/v4/letter/g/b5a626/32.png) [@gitanerie](https://discourse.processing.org/u/gitanerie)\
**Post date:** [September 19, 2021, 7:26pm UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/7 "2021-09-19T19:26:34Z")

</div>

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](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](https://processing.org/reference/round_.html)

---

<div class="post-metadata">

**Author:** ![jafal](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jafal/32/19112_2.png) [@jafal](https://discourse.processing.org/u/jafal)\
**Post date:** [September 20, 2021, 3:37am UTC](https://discourse.processing.org/t/need-help-making-my-code-display-random-set-of-images/32316/8 "2021-09-20T03:37:41Z")

</div>

Hi

General idea close from your demand

[![](https://img.youtube.com/vi/xlLShhnzA14/maxresdefault.jpg "Processing Tutorial: Arrays and Images") ](https://www.youtube.com/watch?v=xlLShhnzA14&t=818s)
