Skip to content

Instantly share code, notes, and snippets.

@ffbit
Created July 29, 2012 10:15
Show Gist options
  • Save ffbit/3197260 to your computer and use it in GitHub Desktop.
Save ffbit/3197260 to your computer and use it in GitHub Desktop.
Simple Random Matrix that was used at Odessa Hackathon 07/2012, Ukraine
import java.util.Arrays;
import java.util.Random;
public class RandomMatrix {
public static void main(String... args) {
double[][][] matrix = getMatrix(40);
print(matrix);
}
private static void print(double[][][] matrix) {
System.out.println(Arrays.deepToString(matrix));
}
private static double[][][] getMatrix(int height) {
double[][][] matrix = new double[height][height][height];
Random random = new Random(System.nanoTime());
for (int k = 0; k < height; k++) {
for (int i = 0; i < height; i++) {
for (int j = 0; j < k; j++) {
double value = random.nextDouble();
if (k > 0) {
if (random.nextBoolean()) {
value *= -0.01;
}
value += matrix[k - 1][i][j];
} else {
value += random.nextInt(Short.MAX_VALUE);
}
matrix[k][i][j] = value;
}
}
}
return matrix;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment