Processing 2.0 Random without duplicate as interger is it possible?

edited October 2013 in How To...

hi there, i have been searching for a big part of the day for a Solution to my problem. I have an Array(10 places) i would like to fill it with int's all random from 1 till 10.. but they cant be the same.

at 1 point i was thinking about using randomSeed put that in a variable use % modulo on it.

if it's even put 2 in array place 1. if its again even add 2 to the previous value and put that in array place 2.

Same goes for uneven.

But is this a good solution. Help me out plz, Thnx

Answers

  • edited October 2013

    You can try IntList structure out.
    When picking a random() value, check out whether that's already stored using hasValue() method!
    If positive, re-pick another value! Use a do...while loop block for it. :-j

  • Answer ✓

    Not sure if I fully understand you but this will create a 10 element array with the values 1-10 (non-repeating) in random positions

    int[] a = new int[10];
    
    // initialise array
    for (int i = 0; i < a.length; i++)
      a[i] = i+1; // 1-10
    
    // randomise array by swapping random elements
    for (int i = 0; i < a.length; i++) {
      int ridx = (int)random(0, 10); // 0-9
      int temp = a[i];
      a[i] = a[ridx];
      a[ridx] = temp;
    } 
    
    // display array
    for (int i = 0; i < a.length; i++)
      print(a[i] + "  ");
    println("");
    
  • Answer ✓

    Well, here's my mentioned solution: :-??

    // forum.processing.org/two/discussion/549/
    // processing-2-0-random-without-duplicate-as-interger-is-it-possible
    
    final int NUM = 10, RANGE = 10;
    final IntList nums = new IntList(NUM);
    
    for (int rnd, i = 0; i != NUM; nums.append(rnd), ++i)
      do    rnd = (int) random(RANGE) + 1;
      while (nums.hasValue(rnd));
    
    
    println(nums);
    exit();
    
  • edited October 2013 Answer ✓

    An even simpler solution is fill up the IntList w/ a continuous range from 1 to RANGE.
    Only then, apply shuffle() method to get random positions outta it! B-)

    // forum.processing.org/two/discussion/549/
    // processing-2-0-random-without-duplicate-as-interger-is-it-possible
    
    final int NUM = 10;
    final IntList nums = new IntList(NUM);
    
    for (int i = 0; i != NUM; nums.append(++i));
    
    nums.shuffle();
    
    println(nums);
    exit();
    

    And after it, you'd prefer a regular Array, use array() method to obtain it outta the IntList! :bz

    // forum.processing.org/two/discussion/549/
    // processing-2-0-random-without-duplicate-as-interger-is-it-possible
    
    final int NUM = 10;
    final IntList nums = new IntList(NUM);
    
    for (int i = 0; i != NUM; nums.append(++i));
    
    nums.shuffle();
    println(nums);
    
    final int[] arr = nums.array();
    println();
    println(arr);
    
    exit();
    
  • I will try them both out, thanks allot

  • edited October 2013

    hi guys,

    A few days ago you helped me on this topic: Processing 2.0 Random without duplicate as interger is it possible?

    you send in your code to shuffle values in an array. could you help me implement it in this.

    greetings,

    Ben

    int value=0;
    int abc = 0;
    int cols = 10;
    int rows = 10;
    int[][] myArray = new int[cols][rows];
    
    void setup() {
      size(550, 550);
    }
    
    void draw() {
      while (abc < 100) {
        background(255, 0, 0);
        for (int i = 0; i < cols; i++) {
          for (int j = 0; j < rows; j++) {
    
            fill(255);
            myArray[i][j] = abc++;
            rect(25+50*i, 50*j, 25, 25);
            rect(width/3,height/18*17,width/3,height/18); // shuffle value
            fill(0);
            text(i+","+j, 30+ 50*j, 20+ 50*i);
            text("shuffle",width/2 -20,height/18*17.7);
            text(myArray[i][j], 30+ 50*j, 40+ 50*i);
          }
        }
      }
    }
    
    void mouseReleased() {
      if (value == 0 && mouseX > width/3 && mouseX < width/3 + width/3 && mouseY > height/18*17 && mouseY < height/18*17 + height/18){  
      //Shuffle Action
    
      }
      for (int i = 0; i < cols; i++) {
        for (int j = 0; j < rows; j++) {
          if (value == 0 && mouseX > 25+(50*i) && mouseX < 25+(50*i) +25 && mouseY > 50*j && mouseY < (50*j)+25) {
            value = 255;
            println("1st value"+ j);
            println("2nd value"+ i );
            fill(0, 255, 0);
            rect(25+(50*i), 50*j, 25, 25);
    
            int getal = myArray[j][i] ;            
            println("number: "+ getal);
    
            if (j == 3 && i == 5) {
              println("oki!");
            }
    
    
            rect(25+(50*i), 50*j, 25, 25);
          }
          else {
            value = 0;
          }
        }
      }
    }
    
  • edited October 2013

    My solutions above involved an IntList rather than a regular Array.
    Only after shuffling, an Array is created outta it!

    Quarks' solution used Array exclusively. However, both of ours were 1D, not 2D! :-SS
    Thus, more thought is needed and I don't have time for it now! X_X

  • This topic is now closed and can be continued here

This discussion has been closed.