How would I write a method that rearranges cards in a deck - java

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

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.

how sum of 2 arraylist?

i got below mentioned error when run code:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.LinkedList.checkPositionIndex(Unknown Source)
at java.util.LinkedList.addAll(Unknown Source)
at Collection.Dynamycmaasiv.Collecktionaddlist.main(Collecktionaddlist.java:36)
code
public static void main(String[] args) {
LinkedList<Integer> num = new LinkedList<Integer>();
LinkedList<Integer> numodd = new LinkedList<Integer>();
LinkedList<Integer> numeven = new LinkedList<Integer>();
LinkedList<Integer> sumoffevenandodd = new LinkedList<Integer>();// help
// me
// to
// solve
for (double i = 0; i < 50; i++) {
num.add((int) i);
if (i % 2 == 0) {
numeven.add((int) i);
} else {
numodd.add((int) i);
}
}
System.out.println(num);
System.out.println("-----------------");
System.out.println(numodd);
System.out.println("-----------------");
System.out.println(numeven);
for (int i =0; i<numeven.size(); i++){
sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null);
}
System.out.println(sumoffevenandodd);
}
}
addAll() is not about adding up numbers. It is about adding all the elements of the method parameter to the collection itself.
So, you need to loop, like
int sum = 0;
for (Integer numberFromList : numeven) {
sum = sum + numberFromList;
Or, if you have Java8, you can use streams:
int sumEven = numeven.stream().sum();
Sum, done.
And for the record: the real lesson to be learned here: read the javadoc. Don't assume that method called addAll() does what you suppose it does. Turn to the javadoc and inform yourself what reality thinks about your assumptions.
But just to be clear; as I got carried away with your question, too.
In your code, if you change
sumoffevenandodd.addAll(numeven.get(i)+ numodd.get(i), null);
to
sumoffevenandodd.add(numeven.get(i)+ numodd.get(i));
it should work, too.
Long story short: if you intended to really have a list with 50 sums within, then my first paragraphs do not really help with your problem.
But it isn't exactly clear what you wanted to do; so I leave my answer as is - to address both possible explanations what is "wrong" in your logic.
if the intention of the question is
num odd
[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]
num even
[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]
sum of odd and even
[1, 5, 9, 13, 17, 21, 25, 29, 33, 37, 41, 45, 49, 53, 57, 61, 65, 69, 73, 77, 81, 85, 89, 93, 97]
then
for (int i =0; i< numeven.size(); i++){
sumoffevenandodd.add(numeven.get(i)+ numodd.get(i));
}

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

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)

Categories