java - Random numbers without duplicate -
this question has answer here:
public class startorder { public static void main(string[] args) { int[] start = new int[45]; for(int i=0;i<start.length;i++) { start[i] = (int)(math.random()*45); } for(int i=0;i<start.length;i++) { system.out.println(start[i]); } } }
i want make simple program prints random numbers between 1-45. problem though there's duplicate numbers among them, how can make print numbers between 1-45 without duplication. it's program generates random start number.
fill list preferred numbers, shuffle it, example using build in library function below.
list<integer> mynumbers = arrays.aslist(1,2,3,4, etc.); collections.shuffle(mynumbers);
then iterate on list.
btw, called random permutation.
Comments
Post a Comment