0

I have generate a random number from 1-100 but not the duplicate number will appear(eg.if 10 comes ones then it will not come again.) (this is not working)

 final Random random = new Random();
    ArrayList<Integer> arrayList = new ArrayList<Integer>();

    while (arrayList.size() < 6) { // how many numbers u need - it will 6
        int a = random.nextInt(91)+1; // this will give numbers between 1 and 90.

        if (!arrayList.contains(a)) {
            arrayList.add(a);
        }
    }

and how can I used that generated appear at the bottom of the screen so that what number is done user must know If 1-100 all the number is done then no number is generated

Can anyone help me with this

Dheeraj Gupta
  • 405
  • 1
  • 4
  • 12
  • check this link : [https://stackoverflow.com/a/8115744/7271027](https://stackoverflow.com/a/8115744/7271027) – Dinesh Apr 15 '20 at 08:41
  • "this is not working" in what way doesn't it work? [Looks to work to me](https://ideone.com/ABjnUU). – Andy Turner Apr 15 '20 at 08:45

2 Answers2

1

Try this may be help you

Random rng = new Random(); // Ideally just create one instance globally
List<Integer> generated = new ArrayList<Integer>();
for (int i = 0; i < numbersNeeded; i++)
{
    while(true)
    {
        Integer next = rng.nextInt(max) + 1;
        if (!generated.contains(next))
        {
            // Done for this iteration
            generated.add(next);
            break;
        }
    }
}
Jignesh Mayani
  • 6,937
  • 1
  • 20
  • 36
0

Here try this.

Integer[] arr = new Integer[100];
    for (int i = 0; i < arr.length; i++) {
        arr[i] = i;
    }
    Collections.shuffle(Arrays.asList(arr));
Parth
  • 791
  • 7
  • 17