Skip to content

Instantly share code, notes, and snippets.

@oyo
Created July 6, 2011 12:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oyo/1067065 to your computer and use it in GitHub Desktop.
Save oyo/1067065 to your computer and use it in GitHub Desktop.
3D pseudorandom number generator (1st try)
import java.util.Random;
public class Random3D {
private long seed;
private Random random;
private long[] masks;
public Random3D(long seed) {
random = new Random(seed);
this.seed = seed;
masks = new long[]{random.nextLong(),random.nextLong(),random.nextLong()};
}
public long getSeed(long x, long y, long z) {
long tx = (x^masks[0])<<7;
long ty = (y^masks[1])<<15;
long tz = (z^masks[2])<<31;
long s = this.seed ^ tx ^ ty ^ tz;
return s;
}
public long getLong(long x, long y, long z) {
random.setSeed(this.getSeed(x,y,z));
return random.nextLong();
}
public static void main(String[] args) {
int dim = 5;
long[][][] cube0 = new long[1][dim][dim];
long[][][] cube1 = new long[1][dim][dim];
Random3D rnd0 = new Random3D(0);
Random3D rnd1 = new Random3D(1);
int x = 0;
for (int y=0;y<dim;y++)
for (int z=0;z<dim;z++) {
cube0[x][y][z] = rnd0.getLong(x,y,z);
cube1[x][y][z] = rnd1.getLong(x,y,z);
}
for (int y=0;y<dim;y++) {
for (int z=0;z<dim;z++)
System.out.print(cube0[x][y][z]+", ");
System.out.println();
}
System.out.println();
for (int y=0;y<dim;y++) {
for (int z=0;z<dim;z++)
System.out.print((cube0[x][y][z]-cube1[x][y][z])+", ");
System.out.println();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment