Help with array (upper bound)

Hi there. I have a question about a sketch i’m working on. I have it working the way I want it to, but, I want to change the amount of images that are displayed randomly from the array. It seems to currently display around 5, and i’m not sure where this is being stipulated in the code and where to change it. I’d like to set the display of the images to 10 random ones from the array. Any help / guidance would be greatly appreciated. Working code below. Thank you


PImage images;

void setup() {
frameRate(1);
size(800, 800);

images = new PImage[32]; // Create an array of 32 images
for (int i = 0; i < images.length; i++) {
images[i] = loadImage( i + “.png”); // Load each image from a file
}
}

void draw() {
background(255);
blendMode(MULTIPLY);
shuffleArray(images); // Shuffle the array of images
for (int i = 0; i < images.length; i++) {
image(images[i], i * random(width), random(height)); // Display each image in a row
}
}

void shuffleArray(PImage array) {
for (int i = array.length - 1; i > 0; i–) {
int j = (int) random(i + 1); // Generate a random index
// Swap the elements at indices i and j
PImage temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}

void keyPressed() {
if (key == ‘s’) {
println(“Saving…”);
saveFrame(“screen-####.jpg”);
println(“Done saving.”);
noLoop();
}
}

This is in the draw() function.

Please try to delete i *.

Hi there, thank you for the reply! Thank worked to a point, I removed the i * and now it displays a lot more! Is there a way I can control the amount of images displayed? So I can stipulate a value ?

Thank you!

found my answer! for (int i = 0; i < 3; i++) {
I just had to change the value here in the loop. Thank you for your help on this! Much appreciated!

1 Like

Good work!

  • 3 is the upper bound.

And welcome to the forum!

Great to see you here!

1 Like