I want to generate some random doubles and add them into an ArrayList, but it seems that the nextDouble() function returns a unique double every time, not a new one
Random r = new Random();
ArrayList<Pair> centers = new ArrayList<Pair>();
ArrayList<ArrayList<Pair>> classes = new ArrayList<ArrayList<Pair>>();
for (int i=0 ; i < 100; i++) {
// Random r = new Random ();
// System.out.println (r.nextDouble ()) ;
double a = r.nextDouble () * 10;
double b = r.nextDouble () * 10;
centers.add (new Pair (a, b ));
System.out.println (centers);
}
Can anyone help me with this? Is this a mistake of optimization?
r = new Random();after having calling it?Random r = new Random(); double a = r.nextDouble() * 10; r = new Random(); double b = r.nextDouble() * 10;nextDoublereturns a uniquedouble, just as it should..double a = new Random().nextDouble * 10;etc. If you want to useamore than once, dodouble b = a;.