I am using random numbers, and I'm generating random numbers between 0 and 7. But there is a problem: random numbers sometimes repeat the previous number.
I am generating a random number on button-click; so on clicking the button I sometime really get a different random number, but sometime it starts repeating numbers. As an example:
On Button click, sometimes the number repeats between the range of 0-7 like:
1-3-4-3-6-3-5-5
As you can see, the 3 is getting repeated multiple times, and the 5 also has two occurrences all together.
So, there are two different types of repetition. I have written the following code, which is supposed to kill the second repetition (the repetition of 5):
int randomNumber;
do {
randomNumber = random.nextInt(7 - 0 + 1) + 0;
} while (randomNumber == lastRandomNumber);
lastRandomNumber = randomNumber;
Log.d("RandomNumber","= "+randomNumber);
return randomNumber;
Now what I want:
It is keeping track of the last random number, but now I want to track all the previous generated random numbers. But how can I do that? What is an optimum way to achieve this? Please guide me or share some code.