How to fill an array with smaller arrays in a random way? - java
I've got an array with 225 elements and 15 smaller arrays which their length sum is exactly 225.
The point is that I need to fill the larger array with these smaller arrays but in a random way.
private final short datosdeNivel[]= new short[225];
private final short diecinueve[]= {19, 19};
private final short veintiseis[]= {26, 26, 26};
private final short dieciocho[]= {18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18};
private final short veintidos[]= {22, 22};
private final short veintiuno[]={21, 21, 21, 21, 21, 21, 21, 21, 21, 21};
private final short cero[]= {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
private final short diecisiete[]= {17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17};
private final short dieciseis[]= {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16,16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16};
private final short veinte[]= {20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20};
private final short veinticuatro[]= {24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24};
private final short veinticinco[]= {25, 25, 25, 25};
private final short veintiocho[]= {28, 28};
private final short uno[]= {1, 1, 1, 1, 1, 1, 1};
private final short nueve[]= {1};
private final short ocho[]= {9, 9, 9, 9, 9, 9, 9, 9, 9};
How can I establish a random order so every time the program runs the order in which the smaller arrays are placed in the larger array is different?
This would be a way to fill it in order:
int aux;
aux= diecinueve.length;
for(int i=0; i<diecinueve.length; i++)
{
datosdeNivel[i]= diecinueve[i];
}
for(int i=0; i<veintiseis.length; i++)
{
datosdeNivel[aux]= veintiseis[i];
aux++;
}
for(int i=0; i<dieciocho.length; i++)
{
datosdeNivel[aux]= dieciocho[i];
aux++;
}
for(int i=0; i<veintidos.length; i++)
{
datosdeNivel[aux]= veintidos[i];
aux++;
}
for(int i=0; i<veintiuno.length; i++)
{
datosdeNivel[aux]= veintiuno[i];
aux++;
}
for(int i=0; i<cero.length; i++)
{
datosdeNivel[aux]= cero[i];
aux++;
}
for(int i=0; i<diecisiete.length; i++)
{
datosdeNivel[aux]= diecisiete[i];
aux++;
}
for(int i=0; i<dieciseis.length; i++)
{
datosdeNivel[aux]= dieciseis[i];
aux++;
}
for(int i=0; i<veinte.length; i++)
{
datosdeNivel[aux]= veinte[i];
aux++;
}
for(int i=0; i<veinticuatro.length; i++)
{
datosdeNivel[aux]= veinticuatro[i];
aux++;
}
for(int i=0; i<veinticinco.length; i++)
{
datosdeNivel[aux]= veinticinco[i];
aux++;
}
for(int i=0; i<veintiocho.length; i++)
{
datosdeNivel[aux]= veintiocho[i];
aux++;
}
for(int i=0; i<uno.length; i++)
{
datosdeNivel[aux]= uno[i];
aux++;
}
for(int i=0; i<nueve.length; i++)
{
datosdeNivel[aux]= nueve[i];
aux++;
}
for(int i=0; i<ocho.length; i++)
{
datosdeNivel[aux]= ocho[i];
aux++;
}
You could try creating an ArrayList, containing all of your smaller arrays:
ArrayList<short[]> arrays = new ArrayList<>();
arrays.add(ocho);
arrays.add(veintiocho);
// ...
and then access indexes randomly, removing from the list each time you add to your large array:
Random rand = new Random();
while (!arrays.isEmpty()) {
int index = rand.nextInt(array.size());
short[] s = arrays.get(index);
for(int i = 0; i < s.length; i++)
{
datosdeNivel[aux]= s[i];
aux++;
}
arrays.remove(index);
}
This will add each array to the large array in a random order.
In pseudo code:
Put the arrays in a List
Shuffle the list using Collections.shuffle(list)
iterate over the list, filling the final array with elements of each array
If I were you, I'll give each array an index, then make a bucket of these indexes, then randomly pick up an index from the bucket, do your copy job, and remove this used index from your bucket.
Related
Add predicted array to trained array
I need to add the predictedData array to the array above for training. The fifth value in the predictedData is going to be predicted. public void machineLearning() throws Exception { Object[][] weatherData = new Object[][]{ {0, 27, 60, 17, 7}, {7, 26, 68, 17, 30}, {30, 27, 57, 14, 14}, {14, 24, 73, 13, 30}, {30, 26, 64, 18, 20}, {20, 27, 62, 17, 18}, {18, 27, 63, 12, 18}, {18, 26, 70, 15, 46}, {46, 26, 66, 18, 33}, {33, 27, 62, 21, 22}, {22, 27, 64, 16, 29}, {29, 26, 62, 15, 23}, {23, 25, 66, 17, 34}, {34, 28, 53, 13, 9}, {9, 28, 66, 18, 10}, {10, 25, 74, 18, 27}, {27, 27, 68, 19, 12}, {12, 26, 70, 12, 29}, {29, 24, 78, 19, 40}, {40, 26, 63, 25, 10}, {10, 25, 66, 18, 18}, {18, 26, 69, 15, 17}, {17, 24, 76, 15, 25}, {25, 24, 80, 11, 31} }; NeuralNet neuralNetwork = new NeuralNet(); //Call the NeuralNetwork class neuralNetwork.readAndTrain(weatherData); //Read and train the data given in weatherDate object neuralNetwork.setupNeuralNet(); //Data to predict Object[][] predictData = new Object[][]{ {30, 27, 70, 18} }; //System.out.println("The new Value is " + neuralNetwork.predictStyle(predictData)); machineTxt.setText(String.valueOf(neuralNetwork.predictStyle(predictData))); }
You can write a method that will grow the array: private static Object[][] addData(Object[][] prevData, Object[][] newData) { int prevDataCount = prevData.length; Object[][] resultData = new Object[prevDataCount + 1][]; System.arraycopy(prevData, 0, resultData, 0, prevData.length); resultData[prevDataCount] = newData[0]; return resultData; } Then call it like that: Object[][] withPrediction = addData( weatherData, predictData); But if you know your arrays will grow you may consider unsing another data structure that allow expansion such as ArrayLists.
Looping through TextView and setting the size
I'm trying to loop through a specified list of sizes and change the size of the text in my TextView accordingly. How can I go about doing this properly? public void TextBigger(View view) { int[] textViewSizes = new int[] {10, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; for(int i=0; i < textViewSizes.length; i++) { text_View.setTextSize(TypedValue.COMPLEX_UNIT_SP, i); } }
//make these class scope int currentTextSize = 0; int[] textViewSizes = new int[] {10, 12, 13, 14, 15, 16, 17, 18, 19, 20 }; public void TextBigger(View view) { if(currentTextSize < textViewSizes.length - 1) currentTextSize++; text_View.setTextSize(TypedValue.COMPLEX_UNIT_SP, textViewSizes[currentTextSize]); }
avoid using java streams- android
I have a code but I want to avoid using java streams because streams are not supported in android. Here is my code: import java.util.Arrays; import static java.util.Arrays.stream; import java.util.concurrent.*; public class VogelsApproximationMethod { final static int[] demand = {30, 20, 70, 30, 60}; final static int[] supply = {50, 60, 50, 50}; final static int[][] costs = {{16, 16, 13, 22, 17}, {14, 14, 13, 19, 15}, {19, 19, 20, 23, 50}, {50, 12, 50, 15, 11}}; final static int nRows = supply.length; final static int nCols = demand.length; static boolean[] rowDone = new boolean[nRows]; static boolean[] colDone = new boolean[nCols]; static int[][] result = new int[nRows][nCols]; static ExecutorService es = Executors.newFixedThreadPool(2); public static void main(String[] args) throws Exception { int supplyLeft = stream(supply).sum(); int totalCost = 0; while (supplyLeft > 0) { int[] cell = nextCell(); int r = cell[0]; int c = cell[1]; int quantity = Math.min(demand[c], supply[r]); demand[c] -= quantity; if (demand[c] == 0) colDone[c] = true; supply[r] -= quantity; if (supply[r] == 0) rowDone[r] = true; result[r][c] = quantity; supplyLeft -= quantity; totalCost += quantity * costs[r][c]; } stream(result).forEach(a -> System.out.println(Arrays.toString(a))); System.out.println("Total cost: " + totalCost); es.shutdown(); } i would be grateful if anyone can help me with this because i can't understand how stream works.
Well that is quite easy, you can use for loop as Mike M suggested in his comment see examples below: int supplyLeft = 0; int[] supply = {50, 60, 50, 50}; for (int i : supply) { supplyLeft += i; } System.out.println(supplyLeft); to replace int supplyLeft = stream(supply).sum(); if you don't want foreach either then int supplyLeft = 0; for (int i = 0; i < supply.length; i++) { supplyLeft += supply[i]; } System.out.println(sum); As far as iterating through 2D array and replacing the java-8 stream as shown below is concerned stream(result).forEach(a -> System.out.println(Arrays.toString(a))); you can use a for loop or Arrays.deepToString() as per your needs see below example: //assume your array is below, you can replace it with results everywhere int[][] costs= { { 16, 16, 13, 22, 17 }, { 14, 14, 13, 19, 15 }, { 19, 19, 20, 23, 50 }, { 50, 12, 50, 15, 11 } }; for (int i = 0; i < costs.length; i++) { System.out.println(Arrays.toString(costs[i])); } Above for loop will print you the array in the following manner [16, 16, 13, 22, 17] [14, 14, 13, 19, 15] [19, 19, 20, 23, 50] [50, 12, 50, 15, 11] If you use Arrays.deepToString(costs) then you get an output as shown below: [[16, 16, 13, 22, 17], [14, 14, 13, 19, 15], [19, 19, 20, 23, 50], [50, 12, 50, 15, 11]] Above to replace
Fixed Point Perlin Noise returns sum of input points as output
I have taken the fixed-point perlin-noise implementation from: http://mrl.nyu.edu/~perlin/noise/INoise.java and modified it slightly so it compiles under c#. In this version 1.0 is represented by 2^16. When I pass in input coordinates (int x, int y, int z) the output is simply the sum of the x and z coordinates. What is wrong with my class or input points? Here is an example of the returned output: for (int y = 0; y < 1; y++) { for (int x = 0; x < 16; x++) { for (int z = 0; z < 16; z++) { Debug.Log(PerlinNoiseFixedPoint.Noise3D(x, y, z)); } } } Output: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30 public class PerlinNoiseFixedPoint { public static int Noise3D(int x, int y, int z) { int X = x >> 16 & 255, Y = y >> 16 & 255, Z = z >> 16 & 255, N = 1 << 16; x &= N - 1; y &= N - 1; z &= N - 1; int u = Fade(x), v = Fade(y), w = Fade(z), A = p[X] + Y, AA = p[A] + Z, AB = p[A + 1] + Z, B = p[X + 1] + Y, BA = p[B] + Z, BB = p[B + 1] + Z; return Lerp(w, Lerp(v, Lerp(u, Grad(p[AA ], x , y , z ), Grad(p[BA ], x-N , y , z )), Lerp(u, Grad(p[AB ], x , y-N , z ), Grad(p[BB ], x-N , y-N , z ))), Lerp(v, Lerp(u, Grad(p[AA+1], x , y , z-N ), Grad(p[BA+1], x-N , y , z-N )), Lerp(u, Grad(p[AB+1], x , y-N , z-N ), Grad(p[BB+1], x-N , y-N , z-N )))); } static int Lerp(int t, int a, int b) { return a + (t * (b - a) >> 12); } static int Grad(int hash, int x, int y, int z) { int h = hash & 15, u = h < 8 ? x : y, v = h < 4 ? y : h == 12 || h == 14 ? x : z; return ((h & 1) == 0 ? u : -u) + ((h & 2) == 0 ? v : -v); } static int Fade(int t) { int t0 = f[t >> 8], t1 = f[Math.Min(255, (t >> 8) + 1)]; return t0 + ( (t & 255) * (t1 - t0) >> 8 ); } static int[] p = new int[512] { 151,160,137,91,90,15, 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180, 151,160,137,91,90,15, 131,13,201,95,96,53,194,233,7,225,140,36,103,30,69,142,8,99,37,240,21,10,23, 190, 6,148,247,120,234,75,0,26,197,62,94,252,219,203,117,35,11,32,57,177,33, 88,237,149,56,87,174,20,125,136,171,168, 68,175,74,165,71,134,139,48,27,166, 77,146,158,231,83,111,229,122,60,211,133,230,220,105,92,41,55,46,245,40,244, 102,143,54, 65,25,63,161, 1,216,80,73,209,76,132,187,208, 89,18,169,200,196, 135,130,116,188,159,86,164,100,109,198,173,186, 3,64,52,217,226,250,124,123, 5,202,38,147,118,126,255,82,85,212,207,206,59,227,47,16,58,17,182,189,28,42, 223,183,170,213,119,248,152, 2,44,154,163, 70,221,153,101,155,167, 43,172,9, 129,22,39,253, 19,98,108,110,79,113,224,232,178,185, 112,104,218,246,97,228, 251,34,242,193,238,210,144,12,191,179,162,241, 81,51,145,235,249,14,239,107, 49,192,214, 31,181,199,106,157,184, 84,204,176,115,121,50,45,127, 4,150,254, 138,236,205,93,222,114,67,29,24,72,243,141,128,195,78,66,215,61,156,180, }; static int[] f = new int[256] { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 2, 3, 3, 4, 6, 7, 9, 10, 12, 14, 17, 19, 22, 25, 29, 32, 36, 40, 45, 49, 54, 60, 65, 71, 77, 84, 91, 98, 105, 113, 121, 130, 139, 148, 158, 167, 178, 188, 199, 211, 222, 234, 247, 259, 273, 286, 300, 314, 329, 344, 359, 374, 390, 407, 424, 441, 458, 476, 494, 512, 531, 550, 570, 589, 609, 630, 651, 672, 693, 715, 737, 759, 782, 805, 828, 851, 875, 899, 923, 948, 973, 998, 1023, 1049, 1074, 1100, 1127, 1153, 1180, 1207, 1234, 1261, 1289, 1316, 1344, 1372, 1400, 1429, 1457, 1486, 1515, 1543, 1572, 1602, 1631, 1660, 1690, 1719, 1749, 1778, 1808, 1838, 1868, 1898, 1928, 1958, 1988, 2018, 2048, 2077, 2107, 2137, 2167, 2197, 2227, 2257, 2287, 2317, 2346, 2376, 2405, 2435, 2464, 2493, 2523, 2552, 2580, 2609, 2638, 2666, 2695, 2723, 2751, 2779, 2806, 2834, 2861, 2888, 2915, 2942, 2968, 2995, 3021, 3046, 3072, 3097, 3122, 3147, 3172, 3196, 3220, 3244, 3267, 3290, 3313, 3336, 3358, 3380, 3402, 3423, 3444, 3465, 3486, 3506, 3525, 3545, 3564, 3583, 3601, 3619, 3637, 3654, 3672, 3688, 3705, 3721, 3736, 3751, 3766, 3781, 3795, 3809, 3822, 3836, 3848, 3861, 3873, 3884, 3896, 3907, 3917, 3928, 3937, 3947, 3956, 3965, 3974, 3982, 3990, 3997, 4004, 4011, 4018, 4024, 4030, 4035, 4041, 4046, 4050, 4055, 4059, 4063, 4066, 4070, 4073, 4076, 4078, 4081, 4083, 4085, 4086, 4088, 4089, 4091, 4092, 4092, 4093, 4094, 4094, 4095, 4095, 4095, 4095, 4095, 4095, 4095, }; }
1.0 IS REPRESENTED BY 2^16 It was not very obvious but the result was intended. Wrapping by function: public static float Noise3D(float x, float y, float z) { //65536 = 2 ^ 16 return Noise3D((int)(x * 65536), (int)(y * 65536), (int)(z * 65536)) / 65536f; } Resulted in following noise: P.S. Code, Just in case
How would I write a method that rearranges cards in a deck
I need to split a deck of cards into two packets: the top half and the bottom half. This new array of cards is suppose to go: first card from from the top packet, first card from bottom packet, second card from top packet, second card from bottom packet, etc. If there are an odd number of cards then the top packet should have one more than the bottom packet. The top of the deck is the front of the array. How would I go about doing this? Here is the method I created to generate the deck of cards (I think it works): private Card[] cards; int value, suit; private final int DECK_SIZE = 52; public Deck() { int index = 0; cards = new Card[DECK_SIZE]; //0 = spades, 1 = hearts, 2 = clovers, 3 =diamonds int suits[] = {0, 1, 2, 3}; //1 = Ace, 11=jack, 12=queen, 13=king int values[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; for (int suit : suits) for (int value : values) { cards[index] = new Card(value, suit); index++; } }
Before you go about doing what you say, note that a perfect shuffle is not a good idea if you are looking to randomize the order of a deck: A perfect faro shuffle, where the cards are perfectly alternated, is considered one of the most difficult sleights of card manipulation, because it requires the shuffler to cut the deck into two equal stacks and apply just the right pressure when pushing the half decks into each other. If one manages to perform eight perfect faro out-shuffles in a row, then the deck of 52 cards will be restored to its original order. If one can do perfect in-shuffles, then 26 shuffles will reverse the order of the deck and 26 more will restore it to its original order. If you want a random shuffle, on the other hand, the way to go is a Fisher-Yates shuffle. From the wikipedia page: To shuffle an array a of n elements (indexes 0..n-1): for i from n − 1 downto 1 do j ← random integer with 0 ≤ j ≤ i exchange a[j] and a[i] Note, however, that depending on your randomness criteria, the standard Java random number generator may not be sufficient: (also from the Wikipedia page:) For example, the built-in pseudorandom number generator provided by many programming languages and/or libraries may often have only 32 bits of internal state, which means it can only produce 232 different sequences of numbers. If such a generator is used to shuffle a deck of 52 playing cards, it can only ever produce a very small fraction of the 52! ≈ 2225.6 possible permutations. It's impossible for a generator with less than 226 bits of internal state to produce all the possible permutations of a 52-card deck. It has been suggested[citation needed] that confidence that the shuffle is unbiased can only be attained with a generator with more than about 250 bits of state. Mersenne Twister is a well-known random number generator that would be adequate. edit: for a literal answer to your original question, here's how I would probably do it (including a test method): import java.util.Arrays; public class Shuffle { /* assumes input and output arrays are same length (N) */ static public <T> void perfectShuffle(T[] input, T[] output, int N) { int itop = 0; int ibottom = N - (N/2); /* bottom has (N/2) elements; for odd N this is rounded down, * and the top part has 1 more element */ int k = 0; while (ibottom < N) { output[k++] = input[itop++]; output[k++] = input[ibottom++]; } // handle last element for N = odd if (k < N) output[k] = input[itop]; } public static void main(String[] args) { int N = 19; String[] in = new String[N]; String[] out = new String[N]; for (int i = 0; i < N; ++i) in[i] = Integer.toString(i); perfectShuffle(in, out, N); System.out.println(Arrays.asList(out)); } } output of main(): [0, 10, 1, 11, 2, 12, 3, 13, 4, 14, 5, 15, 6, 16, 7, 17, 8, 18, 9] finally, the reason why you shouldn't use this for shuffling cards: public static void main(String[] args) { int N = 52; String[] in = new String[N]; String[] out = new String[N]; for (int i = 0; i < N; ++i) in[i] = Integer.toString(i); for (int k = 0; k < 8; ++k) { perfectShuffle(in, out, N); System.out.println(Arrays.asList(out)); String[] tmp = in; in = out; out = tmp; } } output: [0, 26, 1, 27, 2, 28, 3, 29, 4, 30, 5, 31, 6, 32, 7, 33, 8, 34, 9, 35, 10, 36, 11, 37, 12, 38, 13, 39, 14, 40, 15, 41, 16, 42, 17, 43, 18, 44, 19, 45, 20, 46, 21, 47, 22, 48, 23, 49, 24, 50, 25, 51] [0, 13, 26, 39, 1, 14, 27, 40, 2, 15, 28, 41, 3, 16, 29, 42, 4, 17, 30, 43, 5, 18, 31, 44, 6, 19, 32, 45, 7, 20, 33, 46, 8, 21, 34, 47, 9, 22, 35, 48, 10, 23, 36, 49, 11, 24, 37, 50, 12, 25, 38, 51] [0, 32, 13, 45, 26, 7, 39, 20, 1, 33, 14, 46, 27, 8, 40, 21, 2, 34, 15, 47, 28, 9, 41, 22, 3, 35, 16, 48, 29, 10, 42, 23, 4, 36, 17, 49, 30, 11, 43, 24, 5, 37, 18, 50, 31, 12, 44, 25, 6, 38, 19, 51] [0, 16, 32, 48, 13, 29, 45, 10, 26, 42, 7, 23, 39, 4, 20, 36, 1, 17, 33, 49, 14, 30, 46, 11, 27, 43, 8, 24, 40, 5, 21, 37, 2, 18, 34, 50, 15, 31, 47, 12, 28, 44, 9, 25, 41, 6, 22, 38, 3, 19, 35, 51] [0, 8, 16, 24, 32, 40, 48, 5, 13, 21, 29, 37, 45, 2, 10, 18, 26, 34, 42, 50, 7, 15, 23, 31, 39, 47, 4, 12, 20, 28, 36, 44, 1, 9, 17, 25, 33, 41, 49, 6, 14, 22, 30, 38, 46, 3, 11, 19, 27, 35, 43, 51] [0, 4, 8, 12, 16, 20, 24, 28, 32, 36, 40, 44, 48, 1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 2, 6, 10, 14, 18, 22, 26, 30, 34, 38, 42, 46, 50, 3, 7, 11, 15, 19, 23, 27, 31, 35, 39, 43, 47, 51] [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47, 49, 51] [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51]
If you're able to substitute a non-perfect shuffle, try Collections.shuffle(). Your code would look something like this: List card_list = Arrays.asList(cards); Collections.shuffle(card_list); or as #Mark Peters points out, the more concise: Collections.shuffle(Arrays.asList(cards));
I was looking for something similar (shuffling a JSONArray) in this question: An efficient way to shuffle a JSON array in java? I ended up making my own shuffle method implementing this algorithm. For your example, it would be something like: public Card[] shuffle(Card[] cards) { // Implementing Fisher–Yates shuffle Random rnd = new Random(); for (int i = cards.length() - 1; i >= 0; i--) { int j = rnd.nextInt(i + 1); // Simple swap Card card = cards[j]; cards[j] = cards[i]; cards[i] = card; } return cards; }