Unique random number for elements in array

edited November 2017 in Questions about Code

Unique random number for elements in array

I have to create an array of numbers (five element) that have to be unique

int[] numbers = new int[5];
for (int h = 0; h <= 4; h++)
{
 // how to check this random number is unique
    numbers[h] = (int)random(0, 7);
}
printArray(numbers);

Any clue ? Thank in advance SALUD...

Answers

  • edited October 2014
    // forum.processing.org/two/discussion/7696/
    // unique-random-number-for-elements-in-array
    
    static final int QTY = 5, RANGE = 6;
    final int[] numbers = new int[QTY];
    
    void setup() {
      for (int rnd, i = 0; i != QTY; ++i) {
        numbers[i] = MIN_INT;
        while (contains(rnd = (int)random(RANGE + 1), numbers));
        numbers[i] = rnd;
      }
    
      println(numbers);
      exit();
    }
    
    static final boolean contains(int n, int... nums) {
      for (int i : nums)  if (i == n)  return true;
      return false;
    }
    
  • Well, it works, but... can you comment your code? I prefer to understand it.

    thanks in advance

    SALUD...

  • It's a pretty small code. Could you instead pinpoint where you didn't get it? ;;)

  • edited October 2014

    My questions

    1: numbers[i] = MIN_INT;

    MIN_INT is a built constant that give you the small number -2147483648

    I have comment this line numbers[i] = MIN_INT; and I think the code seem to work well, what are the use of this constant?

    2: boolean contains(int n, int... nums) {…..}

    WHILE reference to the variable contains, I don’t understand how you used it, also the expression (int n, int... nums) surprise me.

    I have debug with some println() and I know how the code work, but not why

    static final int QTY = 5, RANGE = 6;
    final int[] numbers = new int[QTY];
    
    void setup() {
      for (int rnd, i = 0; i != QTY; ++i) {
        numbers[i] = MIN_INT;
       println("first: " + numbers[i]);
        while (contains(rnd = (int)random(RANGE + 1), numbers));
        numbers[i] = rnd;
        println("second: " + numbers[i]);
      }
    
      println(numbers);
      exit();
    }
    
    static final boolean contains(int n, int... nums) {
      println("The n: "+ n); 
    
      for (int i : nums)  if (i == n)  return true;
    
      return false;
    }
    

    The Debug :

    first: -2147483648
    The n: 1
    second: 1
    first: -2147483648
    The n: 0 /////////////why your code jump in this  line?
    The n: 1
    The n: 4
    second: 4
    first: -2147483648
    The n: 3
    second: 3
    first: -2147483648
    The n: 6
    second: 6
    first: -2147483648
    The n: 0
    second: 0
    [0] 1
    [1] 4
    [2] 3
    [3] 6
    [4] 0
    

    thanks a lot

    SALUD...

  • Answer ✓
    1. An int[] array have all its slots equal to 0 when instantiated. Since 0 belongs to the random range, I had to give them some arbitrary value outside that picking range. Commenting out that line would put '0' out of it!

    2. "WHILE reference to the variable contains...". contains() isn't a variable, but a function! It checks whether a given number already exists in the array! while () is a Java keyword which creates a loop: https://processing.org/reference/while.html . It repeats the loop as long condition is true.
      In short, it stops when function contains() return false, denoting the picked number wasn't yet present in the array!

  • you can also convert your array to a Set

  • @dimiro, that's also a good path. However, it won't make the program any shorter, b/c it's still gotta check whether the insertion passed thru' and keep trying to pick another Integer in case of failure!

  • Given that the sample size is such a high proportion of the total possible values, I'd be tempted to do it this way instead:

    import java.util.Collections;
    
    // Generate all the possibilities we require
    ArrayList arr = new ArrayList();
    for (int n = 0; n <= 7; ++n)
    {
      arr.add(n);
    }
    
    // Shuffle
    Collections.shuffle(arr);
    
    // We require five numbers, remove the rest
    while (arr.size() > 5)
    {
      arr.remove(0);
    }
    
    printArray(arr);
    
  • edited October 2014

    Very nice inverted logic there, @Antony74! =D> Just make sure to use generics for Collection data structures.
    Many folks here still think that we gotta use (cast) all the time due to incompetency of Processing's reference page,
    that spread such Java 4 legacy bad practice for so many years! =;

    Anyways, since non-regular arrays are allowed now, how about 1 w/ IntList? : (*)
    https://processing.org/reference/IntList.html

    // forum.processing.org/two/discussion/7696/
    // unique-random-number-for-elements-in-array
    
    final int QTY = 5, RANGE = 10;
    final IntList numbers = IntList.fromRange(RANGE);
    
    numbers.shuffle(this);
    
    print("Removed value(s): ");
    for ( int len = numbers.size(); len-- > QTY;
      print(numbers.remove(len) + ", ") );
    
    println();
    println(numbers);
    exit();
    
  • that wasn't the question.

    plus this is a processing forum and processing has its own random(n, m);

Sign In or Register to comment.