-1

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.

Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
Allay Khalil
  • 674
  • 3
  • 11
  • 31

1 Answers1

6
Integer[] arr = {...};
Collections.shuffle(Arrays.asList(arr));

for Example :

List<Integer> ran_num;
      public static void main(String[] args) {
            Integer[] arr = new Integer[1000];
            for (int i = 0; i < arr.length; i++) {
                arr[i] = i;
            }
            Collections.shuffle(Arrays.asList(arr));

            ran_num=new ArrayList<Integer>(Arrays.toString(arr));

        }

The abvoe code can be found at :Java generating non-repeating random numbers

From the generated list fetch one number and then delete it . Next time get a number from the same list.

Now For you case where you want to fetch new number every time create one extra method:

public static int fetch(){

   int num=ran_num.get(0);
    ran_num.remove(0);

if(ran_num.size()==0)
// call some method to regenerate the list as explained in above code

return num;
}
Community
  • 1
  • 1
ashwinbhy
  • 600
  • 1
  • 8
  • 30
  • 2
    what is this and How can I use ? – Allay Khalil Apr 28 '16 at 09:21
  • 1
    This is another way to obtain random numbers, but I believe it's not what you want. – dquijada Apr 28 '16 at 09:22
  • Sorry allay khalil i misinterpretted the question . You can generate a list with 7 numbers then delete the last selected number from list. Next time get random from the next 6 remaining numbers – ashwinbhy Apr 28 '16 at 09:23
  • 1
    @dquijada this is exactly what he wants – pskink Apr 28 '16 at 09:24
  • 1
    But it is making 7th number known , if someone know last 6 values. Just highlighting the fact. by the way nice use of Shuffle . – Panther Apr 28 '16 at 09:27
  • 1
    @Panther a possible solution to that would be regenerating the list when it uses 80%-90% of the elements – ashwinbhy Apr 28 '16 at 09:29
  • Umm... instead of making a direct copy from [this SO answer](http://stackoverflow.com/a/16000210/1682559) (without even changing the 1000 range to 6 as OP wants..), you could have made a comment referring to this answer, or flag the question as a duplicate.. – Kevin Cruijssen Apr 28 '16 at 09:36
  • Don't copy others answers. If you are going to, give credit to original author – Jitin Kodian Apr 28 '16 at 09:37
  • 1
    @KevinCruijssen i just started answering questions today i will link the credits . – ashwinbhy Apr 28 '16 at 09:37