Unique values from random()

edited December 2014 in Programming Questions

I have an array where I am loading 10 images in it, as one can see below:

int maxImages = 10; // Total # of images
int imageIndex = int(random(10)); // Initial image to be displayed is the first

// Declaring an array of images.
PImage[] images = new PImage[maxImages];

// Loading the images into the array
for (int i = 0; i < images.length; i ++ ) {
    images[i] = loadImage( "assets/images/image" + i + ".jpg" ); 
}

When certain conditions are met I am then using random() to generate a new value and save it in a new variable called imageIndex. As you can see below:

imageIndex = int(random(images.length));

After this is generated I am then calling the image with the new random number in the following way:

gameimages = loadImage( "assets/images/image" + imageIndex + ".jpg" );

The above implementation is working perfectly however random numbers are sometimes being generated more than once, I want them uniquely randomised from 1-10. Can anyone suggest me the best way to go about implementing this ?

Tagged:

Answers

Sign In or Register to comment.