0

I am trying to get non repeat random number from an array of numbers. Each time I try to get random value from that array, it should give me non repeat random number. The previous random values should not be repeated

int[] integer_array = {0,1,2,3,4,5,6,7};
int random_no = should be random number from above array
int random_no2 = should be random number from above array other than random_no
int random_no3 = should be random number from above array other than random_no
                                                                and random_no2

Random no from array can be generated for integer_array.length times.

hsuk
  • 6,770
  • 13
  • 50
  • 80
  • 2
    What you want isn't a sequence of random numbers but a shuffling of the numbers 1..n, which the `Collections.shuffle()` method will do for you. – biziclop Sep 18 '14 at 18:33
  • No, its not like that. Its like once I get one random_no, I don't the same random number again. It looks good for long range, but for short range, it keeps repeating numbers. – hsuk Sep 18 '14 at 18:36
  • Do you just not want the same value twice in a row? – Reinstate Monica -- notmaynard Sep 18 '14 at 18:39
  • This question is not clear. Why can't you just use Collections.shuffle()? After you shuffle the collection you can read them out in their new random order with straight iteration. – Jim Sep 18 '14 at 20:35

2 Answers2

0

Here is my code:

public static int[] getIndices(int maxValue, int numberOfIndices) {
    // The result array.
    int[] res = new int[numberOfIndices];
    Random rng = new Random();
    // A set of already used numbers.
    TreeSet<Integer> was = new TreeSet<>();
    for (int i = 0; i < numberOfIndices; i++) {
        // Generate a new number in range [0..maxValue - i].
        // It is a position of a new index in an array of unused values.
        int cur = rng.nextInt(maxValue - i);
        // Compute its position taking into account all values(used and unused)
        // to obtain the real index.
        for (int prev : was)
            if (cur >= prev)
                cur++;
        // Add this index to the result array.
        was.add(cur);
        res[i] = cur;
    }
    return res;
}

The idea behind it is to generate a position of a new number in array of unused values(this array is not maintained explicitly) and then compute the real index value taking into account already used numbers.
What is good about this method is that it calls nextInt only numberOfIndices times and is guaranteed to generate different numbers regardless of what nextInt returns.

kraskevich
  • 18,368
  • 4
  • 33
  • 45
0
    int[] integer_array = {0, 1, 2, 3, 4, 5, 6, 7};
    Random r = new Random();

    int random_no = r.nextInt(integer_array.length);
    System.out.println(random_no);

    int random_no2;
    do {
      random_no2 = r.nextInt(integer_array.length);
    } while (random_no2 == random_no);
    System.out.println(random_no2);

    int random_no3;
    do {
      random_no3 = r.nextInt(integer_array.length);
    } while (random_no3 == random_no || random_no3 == random_no2);
    System.out.println(random_no3);
Ravi Yenugu
  • 3,895
  • 5
  • 40
  • 58