java - Random numbers without duplicate -


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

Popular posts from this blog

c# - Unity IoC Lifetime per HttpRequest for UserStore -

Change the color of an oval at click in Java AWT -

I am trying to solve the error message 'incompatible ranks 0 and 1 in assignment' in a fortran 95 program. -