Add predicted array to trained array - java

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.

Related

delay timer in gwt on quick sort

I'm writing a quick sort algorithm in GWT, between each iteration I'm trying to print the current status.
This is the method
private int partition(int[] list_of_numbers, int first, int last) {
int pivot = list_of_numbers[first];
int up = first;
int down = last;
do {
while ((up < last) && pivot >= list_of_numbers[up]) {
up++;
}
while (pivot < list_of_numbers[down]) {
down--;
}
if (up < down) {
swap(list_of_numbers, up, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
}
} while (up < down);
swap(list_of_numbers, first, down);
sortedResult.setText( array_to_string(list_of_numbers));
logger.log(Level.SEVERE, sortedResult.getText());
return down;
}
It works really well and we can see from the logger that it puts out the following
30, 3, 23, 7, 77, 46, 62, 91, 89, 22, 48, 96, 32, 40, 95,
30, 3, 23, 7, 22, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 23, 7, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
22, 3, 7, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
7, 3, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 62, 91, 89, 77, 48, 96, 32, 40, 95,
3, 7, 22, 23, 30, 46, 40, 91, 89, 77, 48, 96, 32, 62, 95,
3, 7, 22, 23, 30, 46, 40, 32, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 96, 91, 62, 95,
3, 7, 22, 23, 30, 32, 40, 46, 89, 77, 48, 62, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 77, 48, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 62, 48, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 96, 95,
3, 7, 22, 23, 30, 32, 40, 46, 48, 62, 77, 89, 91, 95, 96,
but on the output to the screen it will only display the last iteration
I would like to put a delay of 1 second after each sort where the user can see the current array.
I'm not sure how to use the timer in this case. As well I don't know if it is changing the value each time so fast that I only see the last one or if it's only changing it at the end.
A quick google search yields a page guiding you through delaying calculations and updating the interface.
You can find it here.

How do I create a method that compares 2 integer arrays and get a percentage of how alike they are?

For a personal project I need to make a method, that compares two integer arrays and outputs how a percentage value of how alike they are. The arrays can be of different lengths from each other.
I.E. these could be examples of arrays I can get:
int[] array1 = {21, 154, 25, 180, 2, 159, 25, 25, 181, 25, 3, 181, 25, 180, 2, 160, 4, 179, 21, 2, 159, 17, 54, 21, 158, 25, 180, 21, 25, 180, 21, 4, 100, 46, 79, 25, 180, 21, 25, 180, 21, 4};
int[] array2 = {21, 154, 25, 180, 2, 159, 25, 25, 181, 25, 3, 181, 25, 180, 2, 160, 4, 179, 17, 54, 167, 25, 180, 21, 25, 180, 21, 4, 100, 46, 79, 25, 180, 21, 25, 180, 21, 4, 100, 50, 83, 25, 180, 21, 25, 180};
As you can see the first part is the same but the last part only has some comparison.
I assume that you have to count number of common elements, and then do something about it.
Your problem is strictly algorithmic one: the easiest solution is sort arrays (n*log(n)) then iterate through them in order get how many elements they have in common (it's possible to do it in one loop so you have n operations). It gives us O(n*log(n) + n) what is (n*log(n)).
But there is better approach - you can use associative arrays, your need then more memory (for this additional arrays), then check how many they have in common. It gives you n for putting in arrays and n for finding how many they have in common. So you have O(n+n) so it's O(n).
public class Compare {
public static Integer min(Integer a, Integer b) {
if (a != null && b != null) {
if (a.intValue() > b.intValue())
return b;
else
return a;
} else
// If any of them is null
return 0;
}
public static void how_many_common(int[] arr1, int[] arr2) {
System.out.println("First arr has " + arr1.length);
System.out.println("Second arr has " + arr2.length);
Map<Integer, Integer> arr1_map = new HashMap<>();
Map<Integer, Integer> arr2_map = new HashMap<>();
// Put first arr in Hash map
for (int i : arr1) {
Integer how_many = arr1_map.get(i);
if (how_many != null) {
arr1_map.put(i, how_many + 1);
} else {
arr1_map.put(i, 1);
}
}
// Put second arr in Hash map
for (int i : arr2) {
Integer how_many = arr2_map.get(i);
if (how_many != null) {
arr2_map.put(i, how_many + 1);
} else {
arr2_map.put(i, 1);
}
}
int sumOfCommon = 0;
for (int i : arr1_map.keySet()) {
sumOfCommon += min(arr1_map.get(i), arr2_map.get(i));
}
System.out.println("Arrays have " + sumOfCommon + " numbers in common");
//Rest of likeliness is for you
}
public static void main(String[] args) {
int[] array1 = {21, 154, 25, 180, 2, 159, 25, 25, 181, 25, 3, 181, 25, 180, 2, 160, 4, 179, 21, 2, 159, 17, 54, 21, 158, 25, 180, 21, 25, 180, 21, 4, 100, 46, 79, 25, 180, 21, 25, 180, 21, 4};
int[] array2 = {21, 154, 25, 180, 2, 159, 25, 25, 181, 25, 3, 181, 25, 180, 2, 160, 4, 179, 17, 54, 167, 25, 180, 21, 25, 180, 21, 4, 100, 46, 79, 25, 180, 21, 25, 180, 21, 4, 100, 50, 83, 25, 180, 21, 25, 180};
how_many_common(array1, array2);
int [] array3 = {1,2,3,4};
int [] array4 = {1,2,3,4};
how_many_common(array3, array4);
int [] array5 = {};
int [] array6 = {1,2,3,4};
how_many_common(array5, array6);
int [] array7 = {5};
int [] array8 = {1,2,3,4};
how_many_common(array7, array8);
}
}
It's completed working solution of second approach.

Generate exact sequence of 35 out of 49 numbers from sample

I was wondering if there is any possibility to find out the RNG for the sequences below and accurately predict future sequences. It's an exact sample of 8 bulks of generated numbers containing reference number, date, time to the second and the outcome of 35 comma separated randomly generated numbers. 35 out of 1 to 49. I assume the seed is somehow related to the time. Can these 35 numbers be predicted in the exact order?
Ciprian
Here's the sequence:
35270592 02.07.2015 16:37:30 1, 11, 21, 14, 10, 25, 20, 12, 27, 36, 28, 46, 2, 13, 23, 6, 30, 40, 18, 34, 24, 3, 5, 38, 8, 9, 15, 19, 47, 16, 41, 35, 43, 26, 33
35270591 02.07.2015 16:34:00 27, 33, 4, 26, 47, 21, 48, 28, 42, 49, 24, 32, 14, 44, 29, 15, 39, 35, 41, 10, 34, 45, 18, 30, 43, 8, 6, 19, 40, 2, 31, 3, 7, 9, 23
35270590 02.07.2015 16:30:30 35, 43, 44, 39, 24, 37, 23, 22, 48, 3, 28, 31, 21, 19, 16, 5, 41, 47, 33, 12, 45, 34, 30, 49, 4, 14, 8, 18, 9, 32, 36, 26, 10, 29, 7
35270589 02.07.2015 16:27:00 14, 48, 18, 32, 22, 27, 26, 1, 4, 2, 6, 21, 12, 24, 30, 47, 36, 42, 45, 35, 34, 23, 11, 8, 7, 25, 17, 46, 33, 40, 19, 49, 15, 44, 13
35270588 02.07.2015 16:23:30 35, 23, 43, 6, 5, 49, 21, 14, 18, 47, 40, 11, 1, 26, 4, 39, 34, 44, 37, 31, 29, 24, 33, 2, 20, 41, 25, 42, 36, 10, 28, 32, 19, 8, 48
35270587 02.07.2015 16:20:00 35, 23, 41, 47, 34, 20, 3, 25, 22, 48, 10, 49, 32, 16, 6, 45, 21, 46, 43, 37, 2, 12, 42, 39, 30, 1, 9, 24, 27, 26, 29, 8, 19, 14, 13
35270586 02.07.2015 16:16:30 46, 48, 26, 8, 36, 25, 23, 39, 1, 30, 43, 6, 29, 28, 5, 41, 40, 17, 21, 2, 38, 35, 9, 24, 19, 20, 32, 34, 45, 13, 47, 16, 11, 14, 15
35270585 02.07.2015 16:13:00 16, 33, 20, 21, 43, 35, 26, 39, 18, 37, 44, 47, 28, 48, 17, 15, 19, 6, 14, 22, 46, 4, 8, 31, 41, 12, 9, 49, 2, 3, 11, 25, 10, 30, 40
See https://security.stackexchange.com/questions/4268/cracking-a-linear-congruential-generator for one thing to try. At least, a failure will eliminate the chance that the underlying generator is linear congruential. Even if it is, that will still leave the problem a to how the initial seed is derived from the number and date-time given.

Java ArrayIndexOutOfBoundsException: 20 multidimensional array

I am a new programmer, I have had a class but haven't been back to school yet, so I am trying to get ahead on my own when I have time b doing the problems at projecteuler.net. I have searched on this site and on google for the solution but all of their fixes are using the wrong variable in the for loops which I checked multiple times.
The exception:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 20
at Euler11.main(Euler11.java:37)
My code:
public class Euler11 {
/**
* #param args
*/
public static void main(String[] args) {
int[][] grid = new int[][] {
{8, 02, 22, 97, 38, 15, 00, 40, 00, 75, 04, 05, 07, 78, 2, 12, 50, 77, 91, 8},
{49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 04, 56, 62, 00},
{81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 03, 49, 13, 36, 65},
{52, 70, 95, 23, 04, 60, 11, 42, 69, 24, 68, 56, 01, 32, 56, 71, 37, 02, 36, 91},
{22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80},
{24, 47, 32, 60, 99, 03, 45, 02, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50},
{32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70},
{67, 26, 20, 68, 02, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21},
{24, 55, 58, 05, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72},
{21, 36, 23, 9, 75, 00, 76, 44, 20, 45, 35, 14, 00, 61, 33, 97, 34, 31, 33, 95},
{78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 03, 80, 04, 62, 16, 14, 9, 53, 56, 92},
{16, 39, 05, 42, 96, 35, 31, 47, 55, 58, 88, 24, 00, 17, 54, 24, 36, 29, 85, 57},
{86, 56, 00, 48, 35, 71, 89, 07, 05, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58},
{19, 80, 81, 68, 05, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 04, 89, 55, 40},
{04, 52, 8, 83, 97, 35, 99, 16, 07, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66},
{88, 36, 68, 87, 57, 62, 20, 72, 03, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69},
{04, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36},
{20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 04, 36, 16},
{20, 73, 35, 29, 78, 31, 90, 01, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 05, 54},
{01, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 01, 89, 19, 67, 48}
};
long product = 0;
long hp = 0;
long vp = 0;
long d1p = 0;
long d2p = 0;
for (int h = 3; h < 23; h++) {
for (int v = 3; v < 23; v++) {
hp = grid[h][v] * grid[h][v + 1] * grid[h][v + 2] * grid[h][v + 3];
vp = grid[h][v] * grid[h + 1][v] * grid[h + 2][v] * grid[h + 3][v];
d1p = grid[h][v] * grid[h + 1][v + 1] * grid[h + 2][v + 2] * grid[h + 3][v + 3];
d2p = grid[h][v] * grid[h - 1][v + 1] * grid[h - 2][v + 2] * grid[h - 3][v + 3];
if (hp > product) {
product = hp;
}
if (vp > product) {
product = vp;
}
if (d1p > product) {
product = d1p;
}
if (d2p > product) {
product = d2p;
}
}
}
}
}
I apologize for any sloppiness in the code and if you have any advice on that I am always willing to accept criticism.
Looking over the documentation for the ArrayIndexOutOfBoundsException, it mentions negative numbers and <= symbols but I do not have any that I have noticed.
I tried setting h = 0 and v = 0 and having them go to 19 instead of 23 and got the exception but it said -1 instead of 20 and was on line 40. The MultiDimensional array was my friends idea and I feel like there is probably an easier way, but I don't know what it could be so I went with his suggestion. If you have a site that you used to find the answer and could link it I would appreciate it.
Thank you ahead of time for any advice you can give me.
I don't want to come up with the answer for you, since this is for your learning, and that wouldn't help out much. But I will give a hint for you:
ArrayOutOfBoundsException means that you tried to access an element of an array that doesn't exist. You've got a 20x20 array, so you can use integers between 0 and 19 to access the elements in the array (Remember that arrays are 0-indexed, meaning they start counting from 0 rather than 1). Think on this: Is your code ever trying to access the array with numbers outside of that range?
These array bounds issues are quite common when starting programming, and they are a little confusing at first. Once you understand them, however, they're trivial mistakes forever after.
Your array is 20x20 elements. Inside code you access indexes with [h+3] (maximum) and [h-3] (minimum). Use the for loops like this:
for (int h = 3; h < 17; h++) {
for (int v = 3; v < 17; v++) {
....
Hope this helps.
You are attempting to access a part of the array that is greater than the size of the array (which is 20 by 20; i.e. its indexes go between 0 and 19), i.e, a part of the array that doesn't exist.
Your loops go between 3 and 23, so on this line:
hp = grid[h][v] * grid[h][v + 1] * grid[h][v + 2] * grid[h][v + 3];
When h=0 and v=20 you attempt to access grid[0][20] and the exception is raised. If the exception wasn't raised you'd go on to access grid[0][21] and grid[0][22] on the next time round the loop; even more wrong; even further outside the array.
When you go between 0 and 19 you get the reverse problem
d2p = grid[h][v] * grid[h - 1][v + 1] * grid[h - 2][v + 2] * grid[h - 3][v + 3];
when h=0 and v=0 grid[h - 1][v + 1] attempts to access grid[0 - 1][1], i.e. grid[-1][1], this also does not exist
Without knowing exactly what your goal is (and not wanting to spoil your learning) I can't advise on what to do instead but you must not access parts of the array that do not exist. But it seems like you are manipulating the "inner region" of the array, possibly you don't want to go the whole way from 0-19, maybe you only want 3-16 (for example, see if that makes sense for the problem you're actually solving)

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;
}

Categories