So i set out to make a little "encoding" program that uses a simple algorithm and so far it all works. i came up with the algorithm, and then found the inverse of it to "decode" the given String.
How it works is that in command line you do "java Diver lock message password". It then takes the ascii values and runs it through the algorithm Z_n = (X_n + Y_n) / 2, giving you an "encoded" string that can then be used in the program arguments on start as "java Driver unlock code password". It takes these values and runs them through X_n = 2Z_n - y_n.
These algorithms work when simply using the lock portion, and i've put the same process at the end of lock that happens in the unlocking process, yet when trying only the locking process, the output is incorrect.
Here's a little snippet as to how i believe it is working
lock Oliver Chipper
unlock ĤƨƤnjƪƮä Chipper
x = message, y = password, z = code
x & y = z
z & y = x
I have a feeling that it has to do with the command line not taking in the symbols that are used as output, but a thorough explanation of what i've done would be great... Thank you!
public class Driver {
private static int[] x; //Message or code
private static int[] y; //Password
public static void main(String[] args) {
int SIZE = args[1].length() + args[2].length();
if (args[0].equals("lock")) {
lock(args, SIZE);
} else if (args[0].equals("unlock")) {
unlock(args, SIZE);
}
}
private static void lock(String[] args, int size) {
x = new int[size];
for (int i = 0; i < args[1].length(); i++) {
x[i] = args[1].charAt(i); //Message to ints
}
y = new int[size];
for (int i = 0; i < args[2].length(); i++) {
y[i] = args[2].charAt(i); //Password to ints
}
//code
int[] z = new int[size];
for (int i = 0; i < size; i++) {
z[i] = ((x[i] + y[i]) * 2); //Locking algorithm
System.out.print((char)z[i]);
}
System.out.println("\n");
for (int i = 0; i < size; i++) {
System.out.print((char)((z[i] / 2) - y[i])); //Unlocking algorithm
}
}
private static void unlock(String[] args, int size) {
x = new int[size];
for (int i = 0; i < args[1].length(); i++) {
x[i] = args[1].charAt(i); //Code to ints
}
y = new int[size];
for (int i = 0; i < args[2].length(); i++) {
y[i] = args[2].charAt(i); //Password to ints
}
for (int i = 0; i < size; i++) {
System.out.print((char)((x[i] / 2) - y[i])); //Unlocking algorithm
}
}}
Related
I can't seem to find what's wrong with my neural net, despite verifying my net based on this example, which suggests my backprop and forward prop is working fine. However, after training on XOR my net returns around 0.5 for the output regardless of the input. In other words, the net seems to be minimizing the error as best it can without seeing any correlation between the input and the output. Since a single iteration of back propagation seems to be working fine, my instinct would suggest the problem lies somehow in the iterations that follow. However, there isn't any obvious problem that would cause this, leaving me quite stumped.
I've looked at other threads where similar problems have arisen, but it seems most of the time their error is either extremely niche to the way they set up their net, or their parameters such as learning rate or epochs is really off. Is anyone familiar with a case like this?
public class Net
{
int[] sizes;
double LEARNING_RATE;
double[][][] weights;
double[][] bias;
Random rand = new Random(); //53489085
public Net(int[] sizes_, double LEARNING_RATE_)
{
LEARNING_RATE = LEARNING_RATE_;
sizes = sizes_;
int numInputs = sizes[0];
double range = 1.0 / Math.sqrt(numInputs);
bias = new double[sizes.length - 1][];
weights = new double[sizes.length - 1][][];
for(int w_layer = 0; w_layer < weights.length; w_layer++)
{
bias[w_layer] = new double[sizes[w_layer+1]];
weights[w_layer] = new double[sizes[w_layer+1]][sizes[w_layer]];
for(int j = 0; j < weights[w_layer].length; j++)
{
bias[w_layer][j] = 2*range*rand.nextDouble() - range;
for(int i = 0; i < weights[w_layer][0].length; i++)
{
weights[w_layer][j][i] = 2*range*rand.nextDouble() - range;
}
}
}
}
public double[] evaluate(double[] image_vector)
{
return forwardPass(image_vector)[sizes.length-1];
}
public double totalError(double[][] expec, double[][] actual)
{
double sum = 0;
for(int i = 0; i < expec.length; i++)
{
sum += error(expec[i], evaluate(actual[i]));
}
return sum / expec.length;
}
private double error(double[] expec, double[] actual)
{
double sum = 0;
for(int i = 0; i < expec.length; i++)
{
double del = expec[i] - actual[i];
sum += 0.5 * del * del;
}
return sum;
}
public void backpropagate(double[][] image_vector, double[][] outputs)
{
double[][][] deltaWeights = new double[weights.length][][];
double[][] deltaBias = new double[weights.length][];
for(int w = 0; w < weights.length; w++)
{
deltaBias[w] = new double[bias[w].length];
deltaWeights[w] = new double[weights[w].length][];
for(int j = 0; j < weights[w].length; j++)
{
deltaWeights[w][j] = new double[weights[w][j].length];
}
}
for(int batch = 0; batch < image_vector.length; batch++)
{
double[][] neuronVals = forwardPass(image_vector[batch]);
/* OUTPUT DELTAS */
int w_layer = weights.length-1;
double[] deltas = new double[weights[w_layer].length];
for(int j = 0; j < weights[w_layer].length; j++)
{
double actual = neuronVals[w_layer + 1][j];
double expec = outputs[batch][j];
double deltaErr = actual - expec;
double deltaSig = actual * (1 - actual);
double delta = deltaErr * deltaSig;
deltas[j] = delta;
deltaBias[w_layer][j] += delta;
for(int i = 0; i < weights[w_layer][0].length; i++)
{
deltaWeights[w_layer][j][i] += delta * neuronVals[w_layer][i];
}
}
w_layer--;
/* REST OF THE DELTAS */
while(w_layer >= 0)
{
double[] nextDeltas = new double[weights[w_layer].length];
for(int j = 0; j < weights[w_layer].length; j++)
{
double outNeur = neuronVals[w_layer+1][j];
double deltaSig = outNeur * (1 - outNeur);
double sum = 0;
for(int i = 0; i < weights[w_layer+1].length; i++)
{
sum += weights[w_layer+1][i][j] * deltas[i];
}
double delta = sum * deltaSig;
nextDeltas[j] = delta;
deltaBias[w_layer][j] += delta;
for(int i = 0; i < weights[w_layer][0].length; i++)
{
deltaWeights[w_layer][j][i] += delta * neuronVals[w_layer][i];
}
}
deltas = nextDeltas;
w_layer--;
}
}
for(int w_layer = 0; w_layer < weights.length; w_layer++)
{
for(int j = 0; j < weights[w_layer].length; j++)
{
deltaBias[w_layer][j] /= (double) image_vector.length;
bias[w_layer][j] -= LEARNING_RATE * deltaBias[w_layer][j];
for(int i = 0; i < weights[w_layer][j].length; i++)
{
deltaWeights[w_layer][j][i] /= (double) image_vector.length; // average of batches
weights[w_layer][j][i] -= LEARNING_RATE * deltaWeights[w_layer][j][i];
}
}
}
}
public double[][] forwardPass(double[] image_vector)
{
double[][] outputs = new double[sizes.length][];
double[] inputs = image_vector;
for(int w = 0; w < weights.length; w++)
{
outputs[w] = inputs;
double[] output = new double[weights[w].length];
for(int j = 0; j < weights[w].length; j++)
{
output[j] = bias[w][j];
for(int i = 0; i < weights[w][j].length; i++)
{
output[j] += weights[w][j][i] * inputs[i];
}
output[j] = sigmoid(output[j]);
}
inputs = output;
}
outputs[outputs.length-1] = inputs.clone();
return outputs;
}
static public double sigmoid(double val)
{
return 1.0 / (1.0 + Math.exp(-val));
}
}
And my XOR class looks like this. It's very unlikely that the error lies in this part given it's simplicity, but I figured it couldn't hurt to post just in case I have some fundamental misunderstanding to how XOR works. My net is set up to take examples in batches, but as you can see below for this particular example I send it batches of one, or effectively not using batches.
public class SingleLayer {
static int numEpochs = 10000;
static double LEARNING_RATE = 0.001;
static int[] sizes = new int[] {2, 2, 1};
public static void main(String[] args)
{
System.out.println("Initializing randomly generate neural net...");
Net n = new Net(sizes, LEARNING_RATE);
System.out.println("Complete!");
System.out.println("Loading dataset...");
double[][] inputs = new double[4][2];
double[][] outputs = new double[4][1];
inputs[0] = new double[] {1, 1};
outputs[0] = new double[] {0};
inputs[1] = new double[] {1, 0};
outputs[1] = new double[] {1};
inputs[2] = new double[] {0, 1};
outputs[2] = new double[] {1};
inputs[3] = new double[] {0, 0};
outputs[3] = new double[] {0};
System.out.println("Complete!");
System.out.println("STARTING ERROR: " + n.totalError(outputs, inputs));
for(int epoch = 0; epoch < numEpochs; epoch++)
{
double[][] in = new double[1][2];
double[][] out = new double[1][1];
int num = (int)(Math.random()*inputs.length);
in[0] = inputs[num];
out[0] = outputs[num];
n.backpropagate(inputs, outputs);
System.out.println("ERROR: " + n.totalError(out, in));
}
System.out.println("Prediction After Training: " + n.evaluate(inputs[0])[0] + " Expected: " + outputs[0][0]);
System.out.println("Prediction After Training: " + n.evaluate(inputs[1])[0] + " Expected: " + outputs[1][0]);
System.out.println("Prediction After Training: " + n.evaluate(inputs[2])[0] + " Expected: " + outputs[2][0]);
System.out.println("Prediction After Training: " + n.evaluate(inputs[3])[0] + " Expected: " + outputs[3][0]);
}
}
Can anyone provide some insight as to what may be wrong? My parameters are pretty well defined and I've followed all the suggestions for how the weights should be initialized and what the learning rate should be etc. Thanks!
You're only presenting the first 3 inputs to your neural network, because the following line is wrong:
int num = (int)(Math.random() * 3);
change that to
int num = (int)(Math.random() * inputs.length);
to use all 4 possible inputs.
I figured it out. I wasn't running enough epochs. That seems a little silly to me but this visualization revealed to me that the net lingers on answers ~0.5 for a long time before reducing the error to less than 0.00001.
I am working on an extremely basic game. However when I try to create the array i am running into errors. The error is index out of bounds. However I thought I fixed it by adding the -1 to make sure I don't go out of bounds. can someone tell me, or give me a clue as to what I did wrong?
package gameProject;
public class world {
int numEnemies, numBosses;
int [][] world = new int[10][10];
public world(){
int[][] world = createArray(10,10);
populateWorld(world);
}
private int[][] createArray(int inX, int inY){
//create the array that holds world values
int[][] world = new int[inX][inY];
//initialize the world array
for(int i = 0; i < world.length - 1; i ++){
for(int j = 0; j < world[0].length - 1; j++){
world[i][j] = 0;
}
}
return world;
}
private void populateWorld(int[][] world){
for(int i = 0; i < world.length - 1; i++){
for(int j = 0; j < world[0].length - 1; i++){
world[i][j] = 0;
}
}
}
}
In your populateWorld method, change
for(int j = 0; j < world[0].length - 1; i++)
to
for(int j = 0; j < world[0].length - 1; j++)
You keep incrementing the wrong counter, going eventually out of its bounds. (10)
(PS: you don't need the length - 1 in your loops' condition, just length would do)
The error is in
for (int j = 0; j < world[0].length - 1; i++)
you should write
for (int j = 0; j < world[0].length - 1; j++)
instead.
Note that you can reduce your code a little bit:
You create the array for member World.world twice. Also the elements of an int array are already initialized to 0 so you don't need to do this explicitly.
You should just do
private int[][] createArray(int inX, int inY) {
int[][] world = new int[inX][inY];
for (int i = 0; i < inX; i++)
for (int j = 0; j < inY; j++)
world[i][j] = 0;
return world;
}
You never actually need to check the length of the world array, because the length was already passed in as a parameter value.
And then also
private void populateWorld(int[][] world) {
for (int i = 0; i < world.length; i++)// v error 'i' should be 'j'
for (int j = 0; j < world[i].length; j++) // <- error on this line
world[i][j] = 0;
}
Your basic problem is that you're incrementing the wrong loop variable.
Why? Because you're far off from any clean code.
Lemme show you how clean coding is done:
class names start with a capital letter, method and variable name with lower case letters
you might prefix your variables with their scope ('m' for member, 'p' for parameter, nothing for local variables). Saves you the all-time-reference to 'this'. Strongly depends on your code style. I highly suggest doing it, keeps your code clean and really easy to debug.
use the final and private keywords where possible
use descriptive variable names. Here especially x and y for loop variables, as you're abstracting a 2d-plane
Some more considerations:
usually games grow more complex. Usually simple primitives (like your int-array) will not suffice for long to store all relevant information. Use classes like Cell
use enums so you can lose magic numbers => coding, reading and debugging made a lot easier
So - after a lot of talk - here's the final code:
package gameproject;
/**
* Use comments like this to describe what the classes purpose is.
* Class comment is the most important one. If you can't tell what a method/variable is doing by its name, you should also comment methods and/or variables!
* #author JayC667
*/
public class World {
/*
* STATIC part of the class - keep separated from object code
*/
// you could/should also put these static classes to their separate files and make em non-static
/**
* Denotes, what a {#linkplain Cell} is occupied with
*/
static public enum CellType {
EMPTY, //
BOSS, //
ENEMY
}
/**
* Represents a single cell within the world. Stores information about its coodrinates (redundant) and its occupator (see {#linkplain CellType})
* #author JayC667
*/
static private class Cell { // use cell to store data for you
public final int mX; // x and y are actually not useful at the moment, you could also remove them
public final int mY;
private CellType mCellType = CellType.EMPTY;
public Cell(final int pX, final int pY) {
mX = pX;
mY = pY;
}
public CellType getCellType() {
return mCellType;
}
public void setCellType(final CellType pCellType) {
mCellType = pCellType;
}
}
// when possible, make methods static, unless you unnecessarily blow up the parameter list
// this is a typical demo for a factory method
static private Cell[][] createWorld(final int pWidth, final int pHeight) {
final Cell[][] newWorld = new Cell[pWidth][pHeight];
for (int y = 0; y < pHeight - 1; y++) {
for (int x = 0; x < pWidth - 1; x++) {
newWorld[y][x] = new Cell(x, y);
}
}
return newWorld;
}
/*
* OBJECT part of the class - keep separated from static code
*/
private final Cell[][] mWorld;
private final int mWorldWidth;
private final int mWorldHeight;
private final int mNumberOfEnemies;
private final int mNumberOfBosses;
public World(final int pWidth, final int pHeight, final int pNumberOfEnemies, final int pNumberOfBosses) {
if (pWidth < 1 || pHeight < 1) throw new IllegalArgumentException("World width and height must be greater than 0!");
if (pNumberOfEnemies < 0 || pNumberOfBosses < 0) throw new IllegalArgumentException("Enemy and boss counts must not be negative!");
if (pWidth * pHeight < pNumberOfEnemies + pNumberOfBosses) throw new IllegalArgumentException("World is too small for all the bad guys!");
mWorldWidth = pWidth;
mWorldHeight = pHeight;
mNumberOfEnemies = pNumberOfEnemies;
mNumberOfBosses = pNumberOfBosses;
mWorld = createWorld(pWidth, pHeight);
populateWorld();
}
// refers to many member variables, so not static (would only blow up parameter list)
private void populateWorld() {
for (int i = 0; i < mNumberOfBosses; i++) {
final Cell c = getRandomCell(CellType.EMPTY);
mWorld[c.mY][c.mX].setCellType(CellType.BOSS);
}
for (int i = 0; i < mNumberOfEnemies; i++) {
final Cell c = getRandomCell(CellType.EMPTY);
mWorld[c.mY][c.mX].setCellType(CellType.ENEMY);
}
}
private Cell getRandomCell(final CellType pCellType) {
while (true) { // TODO not a good, but simple solution; might run infinite loops
final int randomX = (int) (mWorldWidth * Math.random());
final int randomY = (int) (mWorldHeight * Math.random());
if (mWorld[randomY][randomX].getCellType() == pCellType) return new Cell(randomX, randomY);
}
}
}
I am trying to iterate through the following pseudo-code set up, and I seem to be stuck using for loops and case distinctions only. Is there a simple way to go through these?
Edit: To be more clear, I have two 2-dimensional double arrays of coordinates given such that I have PunkteX[i][m] and PunkteY[i][m]. These two arrays will always be of the same length, but the integers i and m are variable. I would like to loop through them, for in order to combine them into a new 2-dimensional array of type Point2D[p]. The length of the Point2D array will be determined by i and m.
int i = punkteX.length; // same as punkteY.length;
int m = punkteX[0].length; // same as punkteY[0].length;
Point2D[] array = new Point2D[i*m];
array[0] = (punkteX[0][m], punkteY[0][m]);
array[1] = (punkteX[1][m], punkteY[1][m]);
array[2] = (punkteX[2][m], punkteY[2][m]);
.
.
array[i-1] = (punkteX[i-1][m], punkteY[i-1][m]);
array[i] = (punkteX[i][m], punkteY[i][m]);
array[i+1] = (punkteX[0][m-1], punkteY[0][m-1]);
array[i+2] = (punkteX[1][m-1], punkteY[1][m-1]);
array[i+3] = (punkteX[2][m-1], punkteY[2][m-1]);
.
.
array[p-1] = (punkteX[i-1][0], punkteY[i-1][0]);
array[p] = (punkteX[i][0], punkteY[i][0]);
My attempt, for the sake of completeness, which only seems to work for i = 3 is the following:
for (int p = 0; p < array.length; ++p) {
for (int m = 0; m < mtimes; ++m) { // mtimes = column length of PointX and PointY Arrays
Point2D[] interimarray = new Point2D[n]; // Global var n = i
for (int i = 0; i < interimarray.length; ++i) {
p = (p+i);
if (i == (interimarray.length-1)) { p -= 1; }
if (p < (array.length)) {
array[p] = new Point2D(xArray[i][(mtimes-m-1)], yArray[i][(mtimes-m-1)]);}
if ((p!= 0) && (i==(n-1))) { p += 1; }}}}
This is still very confusing to me, but this will generate the configuration you're looking for. The p index always increases with each iteration through an i array, so you can just do p++ in that loop. Other than that it's just a nested for loop.
int p = 0;
for(int m = mArray.length - 1; m >= 0; m--) {
for(int i = 0; i < iArray.length; i++) {
System.out.println("p = " + p + ", i = " + i + ", m = " + m);
p++;
}
}
If you are struggling with how to store your new data structure, what you probably want to use is a wrapper class. The wrapper class will hold both of the double coordinate arrays from punkteX and punkteY. This code will take your two arrays, loop through them, and store them in point2DArray, which is of type Point2D, our wrapper class. I have also included an example wrapper class so you see how the data is stored.
int mArrayLength = punkteX[0].length;
int iArrayLength = punkteX.length;
Point2D[] point2DArray = new Point2D[mArrayLength];
int p = 0;
for(int m = 0; m < mArrayLength; m++) {
for(int i = 0; i < iArrayLength; i++) {
point2DArray[p] = new Point2D(punkteX[i][m], punkteY[i][m]);
p++;
}
}
public class Point2D {
private double[] punkteX;
private double[] punkteY;
public Point2D(double[] punkteX, double[] punkteY) {
this.punkteX = punkteX;
this.punkteY = punkteY;
}
public double[] getPunkteX() {
return punkteX;
}
public double[] getPunkteY() {
return punkteY;
}
}
Then if you wanted to get out a specific punkteX and punkteY (say when p = 0), you'd use this on your new data structure:
double[] punkteXValue = point2DArray[0].getPunkteX();
double[] punkteYValue = point2DArray[0].getPunkteY();
Aside: Don't use caps at the front of variable names in Java, it makes them look like class names. Good code conventions will make people much more likely to help you since it will be easier to read.
That's my Solution:
public class Test {
private final static int P = 20;
private final static int M = 4;
public static void main(String[] args) {
int m = 5;
int i = 0;
for (int p=0; p < P; p++)
{
if (p % M == 0)
{
m--;
i = 0;
}
System.out.println ("p = " + p +" i = " + i +" m = " + m );
i++;
}
}
}
How about this
int m = mtimes;
int p = 0;
while (m >= 0) {
int itimes = ...; //row length;
for (int i = 0; i < itimes; i++) {
//
p++;
}
m--;
}
Well I made this method Resize with in my Class Object2D, which is supposed to resize the two-dimensional Color-Array PointInformation of the Object2D, that it is called onto, by a certain Percentage. (I found that easier to do when convertig the 2D-Array into an 1D Array)
public class Object2D
{
int width;
int height;
int ResizePercentage = 100;
Color PointInformation[][];
public void Resize(int Percentage)
{
Color[]temp = Standart_Methods.Reduce2DArray(this.PointInformation);
int temp_width = this.width;
int temp_height = this.height;
double Faktor = (Percentage+100)/100;
this.width = (int) (this.width*Faktor);
this.height = (int) (this.height*Faktor);
this.ResetPointInformation();
Color[]temp2 = Standart_Methods.Reduce2DArray(this.PointInformation);
int SamePixelCount = 0;
Color LastColor = temp[0];
for (int i = 0; i < temp.length; i++)
{
if (temp[i] == LastColor )
{
SamePixelCount += 1;
}
else
{
for (int i2 = (int) (i*Faktor); i == 1; i-- )
//Method Resize will only be called when i*Faktor is going to be 100% = X.0 (An Integer)
{
temp2[i*2-i] = LastColor;
}
SamePixelCount = 0;
}
}
Standart_Methods.PrintArray(temp2);
int a = 10;
int b = 0;
System.out.print(a/b); //No Exeption, Code unreachable!?
}
}
It basically starts at temp[0] and adds 1 to the int SamePixelCount as long as it finds the same Color.
When a different Color is found, the method writes the Color of the former Pixels into the right Places in the temp2 Array.
for (int i = 0; i < temp.length; i++)
{
if (temp[i] == LastColor )
{
SamePixelCount += 1;
}
else
{
for (int i2 = (int) (i*Faktor); i == 1; i-- )
//Method Resize will only be called when i*Faktor is going to be 100% = X.0 (An Integer)
{
temp2[i*2-i] = LastColor;
}
SamePixelCount = 0;
}
}
The correct translation of the manipulated Array temp2 into the Object's PointInformation is still missing, because I wanted to test, if temp2 was correctly resized out of temp, so I did
Standart_Methods.PrintArray(temp2); //the Method works btw
but it just did nothing! And even bader! Everything I put at the place of that command, also didn't!
int a = 10;
int b = 0;
System.out.print(a/b); //No Exeption!
And what is even stranger is, that as soon as I call the Method Resize, somewhere, everything after the Call turns into the same strange unreachable Code!?
I am seriously totally clueless about what could have caused this problem.
Any help would be nice!
dividing by zero certainly will give you ArithmeticException:
public class Test {
public static void main(String[] args) {
int a = 10;
int b = 0;
System.out.println(a/b);
}
}
Exception in thread "main" java.lang.ArithmeticException: / by zero
at Test.main(Test.java:14)
I suggest use eclipse and trace your code using the debugger. Inspect your variables per code line and I'm sure you can figure our what's wrong
For some reason, my method "bishops" runs much faster when called from the main method than from the static initialization block. Is this normal, or a bug?
public class Magic
{
public static void main(String[] args)
{
bishops();
}
public static void bishops()
{
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("bishops.txt")));
BISHOP_SHIFTS = new int[64];
BISHOP_COMBOS = new long[64][];
for (int square = 0; square < 64; square++) {System.out.println("bbb " + square);
int NUMBER = bitCount(BISHOP_ATTACKS[square]);
BISHOP_SHIFTS[square] = 64 - NUMBER;
long x = BISHOP_ATTACKS[square];
long[] MAPS = new long[NUMBER];
for (int n = 0; n < NUMBER; n++) {
int i = bitScan(x);
MAPS[n] = (1L << i);
x -= MAPS[n];
}
int C = 1 << NUMBER;
BISHOP_COMBOS[square] = new long[C];
for (int i = 0; i < C; i++) {
BISHOP_COMBOS[square][i] = 0;
int j = i;
for (int n = 0; n < NUMBER; n++) {
if ((j & 1) == 1)
BISHOP_COMBOS[square][i] |= MAPS[n];
j >>>= 1;
}
//out.println("SQUARE " + square);
//out.println(toBitboardString(BISHOP_COMBOS[square][i]));
//out.println();
}
}
//out.close();
bishopMagics();
}
public static void bishopMagics()
{
BISHOP_MAGICS = new long[64];
Random r = new Random();
for (int square = 0; square < 64; square++) {System.out.println("asdffff " + square);
int i;
int LENGTH = BISHOP_COMBOS[square].length;
long magic;
do {
magic = r.nextLong() & r.nextLong() & r.nextLong();
//final int COUNT = bitCount(BISHOP_MASKS[square]);
boolean[] used = new boolean[LENGTH];
for (int j = 0; j < used.length; j++)
used[j] = false;
for (i = 0; i < LENGTH; i++) {
int index = (int) ((BISHOP_COMBOS[square][i] * magic) >>> BISHOP_SHIFTS[square]);
if (used[index])
break;
else
used[index] = true;
}
} while (i < LENGTH);
BISHOP_MAGICS[square] = magic;
System.out.println(magic);
}
//bishopTable();
}
/*
* Lots of stuff omitted
*/
static
{
//bishops();
}
}
It will run much faster the second time than the first as the JVM warms up (loads class es and compiles code). The static block is always called first.
Try running it twice from the main() or the static block and see how long it takes each time
BTW: I would take out any logging to the console as this can slow down the code dramatically.