0

I have written a method which works to generate one random value of an array, but I don't know how to generate the other two because they all end up just being the same number. Here is what I have written, but it just crashes...

public void getRandomTopics() {
Random random = new Random();

int index = random.nextInt(group_topics.length);

randomOne = group_topics[index];
randomTwo = group_topics[index];
randomThree = group_topics[index];

if(randomOne.equals(randomTwo) || randomOne.equals(randomThree) || randomThree.equals(randomTwo)) {
    isEqual = true;
}
while(isEqual = true){
    randomOne = group_topics[index];
    randomTwo = group_topics[index];
    randomThree = group_topics[index];

}
topicOne = (TextView) findViewById(R.id.textView2);
topicOne.setText(randomOne);

topicTwo = (TextView) findViewById(R.id.textView3);
topicTwo.setText(randomTwo);

topicThree = (TextView) findViewById(R.id.textView4);
topicThree.setText(randomThree);

}

If anyone knows an easier way to do this help is greatly appreciated:)

2
  • You need to create random numbers 3 times and not 1 time only. Commented Feb 17, 2016 at 3:47
  • Well, since all 3 are extracted in parallel using group_topics[index], without changing the value of index in between, why would you expect different values? Commented Feb 17, 2016 at 3:47

1 Answer 1

1

Create 3 different random numbers:

int index1 = random.nextInt(group_topics.length);
int index2 = random.nextInt(group_topics.length);
int index3 = random.nextInt(group_topics.length);

Now use these 3 random numbers instead of the same index that you generate only once.


randomOne = group_topics[index1];
randomTwo = group_topics[index2];
randomThree = group_topics[index3];
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.