5 Replies Latest reply on Jul 26, 2013 7:08 AM by e697c280-00bc-47ab-a0c8-c131b4db31be

    How to generate 10 DIFFERENT random numbers?

      I'm trying to fill an ArrayList with ten different random numbers (out of 20 numbers). I just can't seem to get it right. I've tried many things and I think I'm drifting farther away from the solution as I go on.

      Two requests:

      1) An efficient way to do this.
      2) Point out what goes wrong with my incorrect approach so I learn something.

      THANK YOU!

      The last approach I came up with looks like this. But the loop never ends...

      import java.util.ArrayList;
      import java.util.Random;

      public class RandomDifferent {
      public static void main(String args[]) {

      Random randomNumber = new Random();
      int random;

      ArrayList<Integer> b = new ArrayList<Integer>();

      while (b.size() < 10) {
      for (int y=0; y<10; y++) {
      random = randomNumber.nextInt(20);
      for (int i = 0; i<b.size(); i++) {
      if (b.get(i) != random)

      for (int y=0; y<b.size(); y++) {