# Coding Help Cannot Make Random Images Being Chosen

**URL:** https://discourse.processing.org/t/coding-help-cannot-make-random-images-being-chosen/6057
**Category:** Coding Questions
**Created:** [November 29, 2018, 2:27am UTC](https://discourse.processing.org/t/coding-help-cannot-make-random-images-being-chosen/6057 "2018-11-29T02:27:57Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Etorphine](https://avatars.discourse-cdn.com/v4/letter/e/919ad9/32.png) [@Etorphine](https://discourse.processing.org/u/Etorphine)
#### Post date: [November 29, 2018, 2:27am UTC](https://discourse.processing.org/t/coding-help-cannot-make-random-images-being-chosen/6057/1 "2018-11-29T02:27:57Z")

</div>

```auto
//master variables  
PImage[] randomIMG = new PImage[5];
int numOfIMG;
int rand = 0;
PImage image1,image2,image3,image4,image5;
float height = 100;
float width = 50;
float length = 150;
//imageONE = loadImage ("imageNOW.jpg"); //lines 5-7 are the instances of three different image variables that share the same image file 
//imageTWO = loadImage ("imageNOW.jpg"); 
//imageTHREE = loadImage ("imageNOW.jpg"); 

void setup() {
  
  size(400,400);
  
  randomIMG[0] = loadImage("image1.jpg");
  randomIMG[1] = loadImage("image2.jpg");
  randomIMG[2] = loadImage("image3.jpg");
  randomIMG[3] = loadImage("image4.jpg"); 
  randomIMG[4] = loadImage("image5.jpg");
  
  for (int i = 0; i < numOfIMG; i++) { 
    rand = (int)random(5);
    randomIMG[i] = loadImage("image"+ randomIMG[i]+".jpg");
    //print(randomIMG[i]);
    image(randomIMG[i],40,70);
    print(randomIMG[i]);
 }
}
void draw() { 
  background(0);
  fill(150);
  rect(75,50,height, width);
  rect(230,50,height, width);
}

void mousePressed() { 
  link("http://processingjs.org"); 
}

```

This is the code that I have as follows. I would like to create something that allows me to have a random image be displayed upon running the program that is randomly chosen from an element in the array (image in the list).

I am having trouble displaying the image and getting a random number correspondent to the chosen image to display such image.

I am a COMPLETE novice to processing so please be a little easy.

---

<div class="post-metadata">

### Author: ![Kevin](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kevin/32/2297_2.png) [@Kevin](https://discourse.processing.org/u/Kevin)
#### Post date: [November 29, 2018, 2:59am UTC](https://discourse.processing.org/t/coding-help-cannot-make-random-images-being-chosen/6057/2 "2018-11-29T02:59:29Z")

</div>

This is a good start. If I understand correctly, you’re correctly loading your images here:

```auto
randomIMG[0] = loadImage(“image1.jpg”);
randomIMG[1] = loadImage(“image2.jpg”);
randomIMG[2] = loadImage(“image3.jpg”);
randomIMG[3] = loadImage(“image4.jpg”);
randomIMG[4] = loadImage(“image5.jpg”);

```

(By the way, try to format your code when you post it.)

But then I’m not sure what you’re doing with this `for` loop:

```auto
for (int i = 0; i < numOfIMG; i++) {

```

You don’t need to loop over your array to pick one from it. You can probably just do something like this:

```auto
int randomIndex = (int) random(5);
PImage myChosenImage = randomIMG[randomIndex];

```

Also note that you’re clearing out the images by drawing a background color in the `draw()` function. If you want your image to display, you’ll have to redraw it (or get rid of the background).
