I have a pool of numbers (for example, {3,6,7,11,20}), and I need each number to appear in my collection x times.
My solution was to create a class, let's call it the “element” of two integers (num, numOfAppearnces).
I created a pool of "elements" in an arrayList, and then generated random numbers from 0 to list.size and got the number stored in a random index. when numOfAppearances decreased to 0, I removed this item from the list.
My question is, is there another elegant solution for generating a random number, and not out of range?
source
share