Loading...
Logo
Processing Forum

I made a shuffle for arrays

in Share your Work  •  1 year ago  

I made a shuffle for arrays



// shuffle array

int[] a = new int[3];
// int[] a2 = new int[3];

//
void setup() {
  for (int i=0; i<a.length; i++) {
    a[i] = i+1;
  }
  println("the intial array: ");
  display(a); //displays 1 2 3
  println("----------------------");
  println("randomized array: ");
  randomize(a);
  /*the second display method can display either
   1 2 3 or
   1 3 2 or
   2 1 3 or
   2 3 1 or
   3 1 2 or
   3 2 1
   */
}
//
void draw() {
  if (keyPressed) {
    randomize(a);
  }
}
//
//method for display
void display(int[] arr) {
  for (int i=0; i<arr.length; i++)
    print(arr[i]+" ");
  println();
}
//
//method for randomizing
void randomize (int[] a) {
  for (int k=0; k < a.length; k++) {
    // Goal: swap the value at pos k with a rnd value at pos x.
    // save current value from pos/index k into temp
    int temp = a[k];
    // make rnd index x
    int x = (int)random(0, a.length);   
    // overwrite value at current pos k with value at rnd index x
    a[k]=a[x];
    // finish swapping by giving the old value at pos k to the
    // pos x.
    a[x]=temp;
  }
  display(a);
}



Replies(6)


improved  version :     



Copy code
  1. // shuffle array
  2. int[] a  = new int[3];
  3. int[] a2 = new int[3];
  4. //
  5. void setup() {
  6.   size (200, 200);
  7.   // init
  8.   for (int i=0; i<a.length; i++) {
  9.     a[i] = i+1;
  10.   } // for
  11.   // shuffle
  12.   arrayCopy(a, a2);
  13.   randomize(a2);
  14.   /* the result: either
  15.    1 2 3 or
  16.    1 3 2 or
  17.    2 1 3 or
  18.    2 3 1 or
  19.    3 1 2 or
  20.    3 2 1
  21.    */
  22. } // func
  23. //
  24. void draw() {
  25.   background (0);
  26.   // output
  27.   print("the intial array: ");
  28.   display(a, 19); //displays 1 2 3
  29.   print("randomized array: ");
  30.   display(a2, 39); //displays 1 2 3
  31.   text ("Press any key to shuffle.", 10, 59);
  32.   if (keyPressed || mousePressed) {
  33.     randomize(a2);
  34.   } // if
  35. } // func
  36. //
  37. //method for display
  38. void display(int[] arr, int intLineY) {
  39.   for (int i=0; i<arr.length; i++) {
  40.     print(arr[i]+" ");
  41.     text (arr[i]+" ", i*20+10, intLineY);
  42.   } // for
  43.   println();
  44. } // func
  45. //
  46. //method for randomizing
  47. void randomize (int[] arrMy) {
  48.   for (int k=0; k < arrMy.length; k++) {
  49.     // Goal: swap the value at pos k with a rnd value at pos x.
  50.     // Make rnd index x
  51.     int x = (int)random(0, arrMy.length);    
  52.     // swap pos k and x
  53.     arrMy = swapValues(arrMy, k, x);
  54.   } // for
  55.   // display(a, 39);
  56.   // return arrMy;
  57. } // func
  58. //
  59. int[] swapValues (int[] myArray, int a, int b) {
  60.   // Goal: swap the value at pos a with a value at pos b in
  61.   // Array myArray, return the changed array. 
  62.   // save current value from pos/index a into temp
  63.   int temp=myArray[a];
  64.   // overwrite value at current pos a with value at index b
  65.   myArray[a]=myArray[b];
  66.   // finish swapping by giving the old value at pos a to the
  67.   // pos b.
  68.   myArray[b]=temp;
  69.   // return the changed array
  70.   return myArray;
  71. } // func
  72. //



Or you could use Java's own shuffle 

Collections.shuffle(Arrays.asList(myArray));


I can't get Jo Woods idea to work for me.

@ PhiLho:

I look at
http://bazaar.launchpad.net/~philho/+junk/Processing/view/head:/_QuickExperiments/RandomFill/RandomFill.pde

Copy code
  1. // Generate a list of random indices
  2.  // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle 
  3. // inside-out algorithm 
  4. for (int i = 1; i < indices.length; i++) 
  5. {
  6.  int j = int(random(0, i+1)); 
  7.  indices[i] = indices[j];
  8.  indices[j] = i; 
  9. }

nice but when it's swapping we rather need

Copy code
  1. // Generate a list of random indices
  2. // http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
  3. // inside-out algorithm
  4. for (int i = 1; i < positions.length; i++)
  5. {
  6. int j = int(random(0, i+1));
  7. int dummy = positions[i];
  8. positions[i] = positions[j];
  9. positions[j] = dummy;
  10. }


Greetings, Chrisir


Well, my sketch works, no?
What I did there: instead of filling the array with increasing indexes, then randomizing it by swapping values, I just combined the two operations: I take a random value amongst the previously computed ones, and I put it at the current index, then I put the value that should have been found at this place at the location that is just freed.


ah, I see

I was applying it to an existing list and had difficulties

I see now yours is different

Thanks!