RexDough RexDough - 4 months ago 17
Java Question

How to randomise an image array

I have a image array in which I was wondering before I select an image from it with an id number if I could randomise them so the same image can have a different id number every time the app is run?

My image array:

<array name="PairImages">
<item>@drawable/a</item>
<item>@drawable/b</item>
<item>@drawable/c</item>
<item>@drawable/d</item>
<item>@drawable/e</item>
<item>@drawable/f</item>
<item>@drawable/g</item>
<item>@drawable/h</item>
<item>@drawable/i</item>
<item>@drawable/j</item>
</array>


Code I use to fill in my image views with an image from the array:

ImageView FirstImageCard, SecondImageCard, ThirdImageCard, FourthImageCard, FifthImageCard, SixthImageCard, SeventhImageCard, EightImageCard;

@Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().getDecorView().setSystemUiVisibility(getSystemUiFlags());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

FirstImageCard = (ImageView)findViewById(R.id.imageView);
SecondImageCard = (ImageView)findViewById(R.id.imageView2);
ThirdImageCard = (ImageView)findViewById(R.id.imageView3);
FourthImageCard = (ImageView)findViewById(R.id.imageView4);
FifthImageCard = (ImageView)findViewById(R.id.imageView5);
SixthImageCard = (ImageView)findViewById(R.id.imageView6);
SeventhImageCard = (ImageView)findViewById(R.id.imageView7);
EightImageCard = (ImageView)findViewById(R.id.imageView8);

final TypedArray ImageArray = getResources().obtainTypedArray(R.array.PairImages);

FirstImageCard.setImageResource(ImageArray.getResourceId(0, -1));
SecondImageCard.setImageResource(ImageArray.getResourceId(1, -1));
ThirdImageCard.setImageResource(ImageArray.getResourceId(2, -1));
FourthImageCard.setImageResource(ImageArray.getResourceId(3, -1));
FifthImageCard.setImageResource(ImageArray.getResourceId(4, -1));
SixthImageCard.setImageResource(ImageArray.getResourceId(5, -1));
SeventhImageCard.setImageResource(ImageArray.getResourceId(6, -1));
EightImageCard.setImageResource(ImageArray.getResourceId(7, -1));
}

Answer

You have pretty much all the work done, you just need an array or list to hold your id values and then randomize them. Take a look at shuffle.

Your code will end up being something like:

//...

final TypedArray ImageArray = getResources().obtainTypedArray(R.array.PairImages);

//Just a holder for random numbers. 
//If you add/remove an image make sure you change the 8...
ArrayList<Integer> randomNumbers = new ArrayList<>();
for (int i = 0; i < 8; i++){
    randomNumbers.add(i);
}
Collections.shuffle(randomNumbers);
int i = 0;


FirstImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
SecondImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
ThirdImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
FourthImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
FifthImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
SixthImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
SeventhImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));
EightImageCard.setImageResource(ImageArray.getResourceId(randomNumbers.get(i++), -1));

//...

You can also try to build your array with random numbers right away with something like in this question, but I think the shuffle is alright for now.