1

I have is how to generate random images without repeating , as an example driving in an array 15 images and 7 images to send imageview but that are not repeated .

 int[] img={ R.drawable.ima1,R.drawable.ima2,R.drawable.ima3,R.drawable.ima4,R.drawable.ima5,R.drawable.ima6,R.drawable.ima7,R.drawable.ima8,R.drawable.ima9,R.drawable.ima10,R.drawable.ima11,R.drawable.ima12,R.drawable.ima13,R.drawable.ima14,R.drawable.ima15};
    int [] game= new int[7];
Random numerRan = new Random();
  for (int i=0;i<game.length ;i++)
           {
               int num= numerRan.nextInt(17);
               int x= img[num];
               game[i]=img[num];

           }

            img1.setImageResource(juego[0]);
            img2.setImageResource(juego[1]);
            img3.setImageResource(juego[2]);
            img4.setImageResource(juego[3]);
            img5.setImageResource(juego[4]);
            img6.setImageResource(juego[5]);
            img7.setImageResource(juego[6]);
            img8.setImageResource(juego[7]);

When I press the button control images to the score rando the position that was generated . The problem I have is that images are repeated.

I am using 2 arrangements the first is to save all the images and the second keep the random images that will assign imageview .

1 Answers1

0

Instead of using an int[] array, use a List<Integer> instead, such as an ArrayList<Integer>.

That way, you'll be able to use Collections#shuffle(), which will have the desired effect. It'll randomly permute your list, in a non-repeating way. All you'll have to do later is to iterate on that List.

For more info, please refer to the Collections documentation.


You can also convert that array to a List, like the following:

List<Integer> myList = Arrays.asList(img);

For more info about that, take a look at this question.

Mauker
  • 10,551
  • 7
  • 47
  • 69
  • 1
    thanks for your attention and support and me working properly. Use a list < interger > And then it was only agrege elements generate a method to shuffle . – Cristobal Gastelum Aleman Jun 06 '16 at 04:34
  • public static void shuffle(List array) { int tam= array.size(); for(int i= 0 ; i < tam ; i++){ int s = i+(int)(Math.random()*(tam-i)); Integer temp = array.get(s); array.set(s, array.get(i)); array.set(i, temp); } } – Cristobal Gastelum Aleman Jun 06 '16 at 04:34