I got 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 "element", of two integers (num,numOfAppearnces).
I created a pool of "elements" in arrayList, and then generated a random numbers between 0 to list.size, and got the number stored in the random index. when numOfAppearances decreased to 0, i deleted this element from the list.
my question is, is there any other elegant solution to generate random number, not from a range?
Another Easiest way is to use Windows PowerShell
Get-Random 3,6,7,11,20
That't it
here is simple Python program
Yes, there are shorter ways to achieve what you describe.
For example :
Or (if you don't want to use
Collections.nCopies(x,i)
) :