[Solved] Java – Generate random numbers with limit on how many to generate [duplicate]


I’m not going to give you a full answer because then you wouldn’t learn from it. I am going to answer your question by giving an example of a shuffle that you could then use in your situation. This is a custom method to shuffle an array:

public void shuffle(int[] t){
   int temp = 0;
   int j = 0;
   for(int i = 0; i < t.length; i++){
        j = (int)(Math.random() * t.length);
        temp = t[j];
        t[j] = t[i];
        t[i] = temp;
   }
}

Then you can implement this to your array, let’s say your array is int[] t = {1,1,1,1,2,2,2,2};. Then you would need to use your method to link both:

shuffle(t);

solved Java – Generate random numbers with limit on how many to generate [duplicate]