How can I get the random images displaying to not repeat?

jahmed1472 :

I'm making a generic app using Android Studio; I want to display 4 random images from my array but don't know how prevent an image repeating.

public class answerScreen extends AppCompatActivity {
ImageView imageView, imageView2, imageView3, imageView4;
Random r;
Integer[] images = {
        R.drawable.img1,
        R.drawable.img2,
        R.drawable.img3,
        R.drawable.img4,
        R.drawable.img5,
        R.drawable.img6,
        R.drawable.img7
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_answer_screen);

    imageView = (ImageView) findViewById(R.id.imageView);
    imageView2 = (ImageView) findViewById(R.id.imageView2);
    imageView3 = (ImageView) findViewById(R.id.imageView3);
    imageView4 = (ImageView) findViewById(R.id.imageView4);

    r = new Random();

    //diplay random image
    imageView.setImageResource(images[r.nextInt(images.length)]);
    imageView2.setImageResource(images[r.nextInt(images.length)]);
    imageView3.setImageResource(images[r.nextInt(images.length)]);
    imageView4.setImageResource(images[r.nextInt(images.length)]);
}
}
Eritrean :

You could generate 4 random ints in range [0,images.length) and store them in an array like:

Random r = new Random();
int randInts [] = r.ints(0, images.length).distinct().limit(4).toArray();

and use that array to set image resources:

imageView.setImageResource(images[randInts[0]]);
imageView2.setImageResource(images[randInts[1]]);
imageView3.setImageResource(images[randInts[2]]);
imageView4.setImageResource(images[randInts[3]]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

how i can repeat loop for random walk?

Ho can I get these images to appear in random spots on the page?

ng repeat not displaying images

How not to repeat the random src of the images in jQuery?

how can I get a random generation of yes and no?

How can I get a random number in Kotlin?

How can I get unique random numbers?

How can I get a random streetview location?

How can I display images from a folder in random order with javascript?

How can I scrape text and images from a random web page?

How can i plot random images loaded with ImageDataGenerator?

How can I display and hide images based on a random number generator?

How can I get zoom functionality for images?

How can I get the img displaying full length in mobile?

I want to get random data without repeat

how I can get data from the firebase database without repeat

How do I repeat a random number

How do I get a SKAction to repeat itself after a random period of time

Pygame, how do I get a random output to repeat the same value twice?

How can I get random time zone in python

How can I get full-ranged random float values?

How can I get one random image from Parse for iOS?

How can I get random rows in MySQL (NO autoincrement)?

How can I get a random number in Rust 1.0?

How can i get a random user from mongodb?

How can I get random numbers between -1024 and 1024 in vhdl

random choice: how can I get a weighted presentation for x trials?

How Can I Parse String and Get Random Sentences in C#?

How can i get random colors for each object in canvas with JS?