0

How can I implement an integer random, that can generate each number just one time but without repetition. I use this code in Android.

int random = Random.nextInt((max+1 - min) + min;

Problem: between max and min there is repeated numbers and also some numbers, they don't exist.

Sam Joos
  • 430
  • 2
  • 11
  • 25
  • What do you need this for? In any case, you don't want to use `Random`, you should see [this post](https://stackoverflow.com/questions/5505927/how-to-generate-a-random-permutation-in-java) – F. Stephen Q Sep 04 '15 at 04:37

2 Answers2

3

You need a shuffle algorithm

https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle

create an array/list of the possible number, shuffle it, and then get the number once at a time

Derek Fung
  • 8,171
  • 1
  • 25
  • 28
0

instead of having a list (as mentioned from Derek Fung) with all possible values you

  • can store created values in an list
  • check if any new created value is in that list and then
    • add it to the list
    • or create a new value until you found one that is not in the list
Martin Frank
  • 3,445
  • 1
  • 27
  • 47