How to randomize order of array

edited December 2016 in Using Processing

So far, this creates 16 ellipses that are equidistant in hue. Is there any way to create a new array of colors based off the pre-existing one where the colors are in a random order, and then plug in that array for the ellipses? Thanks in advance!

color[] colors = new color[16];
int k;

void setup() {
  colorMode(HSB, 16, 100, 100);
  size(800, 950);
  background(0, 0, 100);
  for (k=0; k<16; k++) {
    colors[k] = color(k+1, 75, 100);

   }

}  


void draw() {
  int k= 0;
  for (float i = 150; i < 701; i = i+166) {
    for (float j = 150; j < 701; j = j+166) {
      noStroke();
     // int index = int(random(colors.length));
      fill(colors[k]);
      ellipse(i, j, 100, 100);
      k++;
    }
  }
}
Tagged:

Answers

  • _vk_vk
    edited March 2014 Answer ✓

    I think You could use Collections.shuffle, but as color is an int and lists can't have primitives there is some extra work to be done

    code edited - added "press any key to re-sort" : )

    import java.util.Collections;
    
    ArrayList <Integer> colors = new ArrayList<Integer>();
    int k;
    
    void setup() {
      colorMode(HSB, 16, 100, 100);
      size(800, 950);
      background(0, 0, 100);
      for (k=0; k<16; k++) {
        colors.add( (Integer)color(k+1, 75, 100));
      }
      Collections.shuffle(colors);
      noLoop();
    }  
    
    
    void draw() {
      background(0, 0, 100);
      int k= 0;
      for (float i = 150; i < 701; i = i+166) {
        for (float j = 150; j < 701; j = j+166) {
          noStroke();
          // int index = int(random(colors.length));
          fill(colors.get(k));
          ellipse(i, j, 100, 100);
          k++;
        }
      }
    }
    
    
    // click to re-shuffle...
    void mousePressed() {
    
      Collections.shuffle(colors);
      redraw();
    }
    
    
    // any key to re-sort...
    void keyReleased() {
     
      Collections.sort(colors);
      redraw();
    }
    

    or you can make your won shuffle function, like the one below : )

    import java.util.Random;
    import java.util.Arrays;
    
    
    color[] colors = new color[16];
    int k;
    
    void setup() {
      colorMode(HSB, 16, 100, 100);
      size(800, 950);
      background(0, 0, 100);
    
      for (k=0; k<16; k++) {
        colors[k] = color(k+1, 75, 100);
      }
    
      shuffleArray(colors);
      noLoop();
    }  
    
    
    void draw() {
      background(0, 0, 100);
      int k= 0;
      for (float i = 150; i < 701; i = i+166) {
        for (float j = 150; j < 701; j = j+166) {
          noStroke();
          fill(colors[k]);
          ellipse(i, j, 100, 100);
          k++;
        }
      }
    }
    
    
    
    // click to re-shuffle...
    void mousePressed() {
    
      shuffleArray(colors);
      redraw();
    }
    
    // any key to re-sort...
    void keyReleased() {
    
      Arrays.sort(colors);
      redraw();
    }
    
    
    
    
    void shuffleArray(int[] array) {
    
      // with code from WikiPedia; Fisher–Yates shuffle 
      //@ http://en.wikipedia.org/wiki/Fisher–Yates_shuffle
    
      Random rng = new Random();
    
      // i is the number of items remaining to be shuffled.
      for (int i = array.length; i > 1; i--) {
    
        // Pick a random element to swap with the i-th element.
        int j = rng.nextInt(i);  // 0 <= j <= i-1 (0-based array)
    
        // Swap array elements.
        int tmp = array[j];
        array[j] = array[i-1];
        array[i-1] = tmp;
      }
    }
    
  • _vk_vk
    edited March 2014

    Edited the code above to add a "re-sort" option. Only in Collections approach.

  • Edited again, now in both approaches : )

  • edited March 2014 Answer ✓

    A simpler lazy approach for a free shuffle() method is by using the new Processing 2+'s data structure IntList:

    http://processing.org/reference/IntList.html

    // forum.processing.org/two/discussion/3546/how-to-randomize-order-of-array
    
    final int NUM = 16;
    final IntList colors = new IntList(NUM);
    
    for ( int k = 0; k++ != NUM; colors.append(color(k, 75, 100)) );
    println(colors);
    
    colors.shuffle(this);
    println();
    println(colors);
    
    exit();
    
Sign In or Register to comment.