Java Array Shuffle shuffle(double[][] kernel, long seed)

Here you can find the source of shuffle(double[][] kernel, long seed)

Description

Shuffle a kernel matrix with seed to initialize the Random object

License

Open Source License

Parameter

Parameter Description

Return

the shuffled kernel matrix

Declaration

public static double[][] shuffle(double[][] kernel, long seed) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.Arrays;
import java.util.Collections;
import java.util.Random;

public class Main {
    /**// www .j a  v a 2 s.  co m
     * Shuffle a kernel matrix with seed to initialize the Random object
     * 
     * @param kernel, 2D matrix of doubles
     * @param seed, for the Random object
     * @return the shuffled kernel matrix
     */
    public static double[][] shuffle(double[][] kernel, long seed) {
        Double[][] kernelDouble = convert2DoubleObjects(kernel);
        for (int i = 0; i < kernel.length; i++) {
            Collections.shuffle(Arrays.asList(kernelDouble[i]), new Random(seed));
        }
        Collections.shuffle(Arrays.asList(kernelDouble), new Random(seed));
        return convert2DoublePrimitives(kernelDouble);
    }

    private static Double[][] convert2DoubleObjects(double[][] kernel) {
        Double[][] kernelDouble = new Double[kernel.length][kernel[0].length];

        for (int i = 0; i < kernel.length; i++) {
            for (int j = 0; j < kernel[i].length; j++) {
                kernelDouble[i][j] = new Double(kernel[i][j]);
            }
        }
        return kernelDouble;
    }

    private static double[][] convert2DoublePrimitives(Double[][] kernelDouble) {
        double[][] kernel = new double[kernelDouble.length][kernelDouble[0].length];

        for (int i = 0; i < kernelDouble.length; i++) {
            for (int j = 0; j < kernelDouble[i].length; j++) {
                kernel[i][j] = kernelDouble[i][j].doubleValue();
            }
        }
        return kernel;
    }
}

Related

  1. shuffle(Collection items, Random random)
  2. shuffle(Collection objects)
  3. shuffle(double[] arr, Random rnd)
  4. Shuffle(double[] array)
  5. shuffle(double[] array, Random r)
  6. shuffle(final A source, final Random random)
  7. shuffle(final Object[] array)
  8. shuffle(final Object[] values)
  9. shuffle(final T[] arr)