Loading...
Logo
Processing Forum

Help with Random images!

in General Discussion  •  Other  •  8 months ago  
I'm incredibly new to working with Processing and I have a project I'm working on for a class that I don't fully understand.

They want us to create and name 5 image variables with the last one being currentImage, apparently so we can use it to randomize the images selected.

Any ideas on how I might go about doing that?

Replies(1)

If you save your 5 images like this:

someImage0, someImage1, someImage2, someImage3, and someImage4

Then you would first use random() to pick a number between 0 - 4 (inclusive). Because random returns a float (a number with decimal / fractional information) you will have to type cast it as an integer like this:
Copy code
  1. int randNum = (int)random(5);

That number will correspond to the image you want to select. Then you can concatenate that number to a String like this:
Copy code
  1. String currentImage = "someNumber"+randNum;

Edit: I forgot to mention that you will have to also concatenate the file extension to the end of the line of code above.