SHARE
TWEET

Square Permutation Thingy

a guest Oct 14th, 2016 97 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.Arrays;
  2. import java.util.Random;
  3.  
  4. public class SquarePermutations
  5. {
  6.   public static void main(String[] args)
  7.   {
  8.     final int size = 9;
  9.     Random rng = new Random(1); // Constant seed. Change if you want.
  10.     int[] lut1, lut2, lut3;
  11.     lut1 = generateTable(size, rng);
  12.     lut2 = generateTable(size, rng);
  13.     lut3 = generateTable(size, rng);
  14.    
  15.     for (int r = 0; r < size; r++)
  16.     {
  17.       for (int c = 0; c < size; c++)
  18.       {
  19.         int n = lut1[r];
  20.         n = lut2[(n + c) % size];
  21.         n = lut3[n];
  22.        
  23.         System.out.print((n + 1) + " ");
  24.       }
  25.      
  26.       System.out.println();
  27.     }
  28.   }
  29.  
  30.   public static int[] generateTable(int size, Random rng)
  31.   {
  32.     int[] table = new int[size];
  33.     for(int i = 0; i < size; i++) table[i] = i;
  34.    
  35.     // Fisher yates shuffle permutes our look up tables
  36.     for (int i = size; i > 1; i--)
  37.     {
  38.       int r = rng.nextInt(i);
  39.       int temp = table[i - 1];
  40.       table[i - 1] = table[r];
  41.       table[r] = temp;
  42.     }
  43.    
  44.     return table;
  45.   }
  46. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top