Java Random randomPermute(List l, Random rand)

Here you can find the source of randomPermute(List l, Random rand)

Description

random Permute

License

Open Source License

Declaration

public static <T> void randomPermute(List<T> l, Random rand) 

Method Source Code


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

import java.util.*;

public class Main {
    public static <T> void randomPermute(List<T> l, Random rand) {
        for (int i = 0; i < l.size(); i++) {
            int j = i + rand.nextInt(l.size() - i);
            T x = l.get(i);/*  ww w  .  ja v a2 s  . c om*/
            l.set(i, l.get(j));
            l.set(j, x);
        }
    }

    public static <T> T get(List<T> l, int i) {
        return get(l, i, null);
    }

    public static <T> T get(List<T> l, int i, T defValue) {
        if (i < 0)
            i += l.size();
        if (i < 0 || i >= l.size())
            return defValue;
        return l.get(i);
    }

    public static <T> T get(T[] l, int i) {
        return get(l, i, null);
    }

    public static <T> T get(T[] l, int i, T defValue) {
        if (i < 0)
            i += l.length;
        if (i < 0 || i >= l.length)
            return defValue;
        return l[i];
    }

    public static double get(double[] l, int i, double defValue) {
        if (i < 0)
            i += l.length;
        if (i < 0 || i >= l.length)
            return defValue;
        return l[i];
    }

    public static int[] set(int[] v, int x) {
        for (int i = 0; i < v.length; i++)
            v[i] = x;
        return v;
    }

    public static int[] set(int[] v, int x[], int n) {
        for (int i = 0; i < n; i++)
            v[i] = x[i];
        return v;
    }

    public static int[] set(int[] v, int x[]) {
        return set(v, x, v.length);
    }

    public static double[] set(double[] v, double x[]) {
        for (int i = 0; i < v.length; i++)
            v[i] = x[i];
        return v;
    }

    public static double[] set(double[] v, double x) {
        for (int i = 0; i < v.length; i++)
            v[i] = x;
        return v;
    }

    public static double[][] set(double[][] v, double[][] x) {
        for (int i = 0; i < v.length; i++)
            for (int j = 0; j < v[i].length; j++)
                v[i][j] = x[i][j];
        return v;
    }

    public static double[][] set(double[][] v, double x) {
        for (int i = 0; i < v.length; i++)
            set(v[i], x);
        return v;
    }

    public static double[][][] set(double[][][] v, double x) {
        for (int i = 0; i < v.length; i++)
            set(v[i], x);
        return v;
    }

    public static double[][][][] set(double[][][][] v, double x) {
        for (int i = 0; i < v.length; i++)
            set(v[i], x);
        return v;
    }

    public static double[][][][][] set(double[][][][][] v, double x) {
        for (int i = 0; i < v.length; i++)
            set(v[i], x);
        return v;
    }
}

Related

  1. randomNanoTime()
  2. randomNick()
  3. randomPassword()
  4. randomPermutation(int size)
  5. randomPermutations(int[] tab, Random r)
  6. randomProbability(double probability)
  7. randomPseudo()
  8. randomRange(int end)
  9. randomScalingFactor()