hectichavana
hectichavana

Reputation: 1446

android: randomize JSON array number

setImage(cachedImage1, image1, prjcts.get(x)); setImage(cachedImage2, image2, prjcts.get(x); setImage(cachedImage3, image3, prjcts.get(x));

I need to replace x from prjcts.get(x) with a random number between 1 until the maximum entry on my JSON file. The maximum number should be felxible, I can add and delete the entry as much as I want. And one more thing, the numbers aren't allowed to be the same between each other.

Can anybody help me with this?

Thank you very much.

Upvotes: 0

Views: 574

Answers (1)

Haphazard
Haphazard

Reputation: 10948

Try creating a List list and fill it with values from 1 to jsonMax. Then use Java Collections.shuffle(..) to randomize the list.

List<Integer> list = new ArrayList<Integer>();
int jsonMax = getMaxFromJSON(); //supplied by you
for(int i = 1; i<=jsonMax; i++)
    list.add(i);

Collections.shuffle(list);

Increment through list to get the random numbers.

Upvotes: 1

Related Questions