vivek_Android
vivek_Android

Reputation: 1697

Android Random Number

I m generating one random card from array. and assigning it.' Below is the code..but its is showing an error. What is the problem?

public void rand() {
    String rank[]=  {"tclub1.png", "tclub2.png", "tclub3.png", "tclub4.png", "tclub5.png", "tclub6.png", "tclub7.png", "tclub8.png", "tclub9.png", "tclub10.png","tclub11.png", "tclub12.png", "tclub13.png"};

    Random randInt = new Random();

    int b = randInt.nextInt((rank.length));
    showcard1.setBackgroundResource(b); 
}

Upvotes: 3

Views: 9072

Answers (2)

3rgo
3rgo

Reputation: 3153

Try changing to int b = randInt.nextInt((rank.length)) - 1; (because rank.length = 13 and your array is indexed from 0 to 12)

Upvotes: 3

Jason Byrne
Jason Byrne

Reputation: 41

b is an int

so you need rank[b] the array at some point in your code

According to your code maybe it should read showcard1.setBackgroundResource(rank[b]);

Upvotes: 4

Related Questions