Randomly permutes the elements of the specified array : Random « Development Class « Java






Randomly permutes the elements of the specified array

  
/*
 * LingPipe v. 3.9
 * Copyright (C) 2003-2010 Alias-i
 *
 * This program is licensed under the Alias-i Royalty Free License
 * Version 1 WITHOUT ANY WARRANTY, without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the Alias-i
 * Royalty Free License Version 1 for more details.
 *
 * You should have received a copy of the Alias-i Royalty Free License
 * Version 1 along with this program; if not, visit
 * http://alias-i.com/lingpipe/licenses/lingpipe-license-1.txt or contact
 * Alias-i, Inc. at 181 North 11th Street, Suite 401, Brooklyn, NY 11211,
 * +1 (718) 290-9170.
 */

//package com.aliasi.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Static utility methods for processing arrays.
 *
 * @author  Bob Carpenter
 * @version 4.0.0
 * @since   LingPipe1.0
 */
public class Arrays {



    /**
     * Return a shallow copy of the specified array that
     * contains the same elements as the specified array.  If
     * the input is <code>null</code>, then <code>null</code>
     * is returned.
     *
     * @param cs Array to copy.
     * @return Shallow copy of array.
     */
    public static char[] copy(char[] cs) {
        if (cs == null) return null;
        char[] cs2 = new char[cs.length];
        for (int i = 0; i < cs.length; ++i)
            cs2[i] = cs[i];
        return cs2;
    }


    /**
     * Converts the specified character sequence to an array
     * of characters.
     *
     * @param cSeq Character sequence to convert.
     * @return Array of characters in sequence.
     */
    public static char[] toArray(CharSequence cSeq) {
        // return cSeq.toString().toCharArray();
        char[] cs = new char[cSeq.length()];
        for (int i = 0; i < cs.length; ++i)
            cs[i] = cSeq.charAt(i);
        return cs;
    }


    /**
     * Returns true if the specified object is an element of the
     * specified array.  Returns <code>false</code> if the specified
     * array is null.
     *
     * @param x Object to test for membership.
     * @param xs Array to test for object.
     * @return <code>true</code> if the specified object is an element
     * of the specified array.
     */
    public static boolean member(Object x, Object[] xs) {
        if (xs == null) return false;
        for (int i = xs.length; --i >= 0; ) {
            if (xs[i] == null) continue;
            if (xs[i].equals(x)) return true;
        }
        return false;
    }

    /**
     * Returns true if the specified character is a member of the
     * specified array.  Returns <code>false</code> if the specified
     * array is null.
     *
     * @param c Character to test for membership.
     * @param cs Array to test for character.
     * @return <code>true</code> if the specified character is an
     * element of the specified array.
     */
    public static boolean member(char c, char[] cs) {
        if (cs == null) return false;
        for (int i = 0; i < cs.length; ++i) {
            if (cs[i] == c) return true;
        }
        return false;
    }

    /**
     * Returns the concatenation of the string representations of the
     * specified objects separated by commas, with the whole
     * surrounded by square brackets and separated by a comma.
     *
     * @param xs Array of which to return a string representation.
     * @return String representation of the specified array.
     */
    public static String arrayToString(Object[] xs) {
        StringBuilder sb = new StringBuilder();
        arrayToStringBuilder(sb,xs);
        return sb.toString();
    }

    /**
     * Appends to the string buffer the concatenation of the string
     * representations of the specified objects separated by commas,
     * with the whole surrounded by square brackets and separated by a
     * comma.
     *
     * @param sb String buffer to which string representation is
     * appended.
     * @param xs Array of which to return a string representation.
     */
    public static void arrayToStringBuilder(StringBuilder sb, Object[] xs) {
        sb.append('[');
        for (int i = 0; i < xs.length; ++i) {
            if (i > 0) sb.append(',');
            sb.append(xs[i]);
        }
        sb.append(']');
    }

    /**
     * Return <code>true</code> if the specified arrays are
     * the same length and contain the same elements.
     *
     * @param xs First array.
     * @param ys Second array.
     * @return <code>true</code> if the specified arrays are the same
     * length and contain the same elements.
     */
    public static boolean equals(Object[] xs, Object[] ys) {
        if (xs.length != ys.length) return false;
        for (int i = 0; i < xs.length; ++i)
            if (!xs[i].equals(ys[i])) return false;
        return true;
    }

    /**
     * Randomly permutes the elements of the specified array using
     * a freshly generated randomizer.  The
     * resulting array will have the same elements, but arranged into
     * a (possibly) different order.
     *
     * @param xs Array to permute.
     * @param <E> the type of objects in the array being permuted
     */
    public static <E> void permute(E[] xs) {
        permute(xs,new Random());
    }

    /**
     * Randomly permutes the elements of the specified array using the
     * specified randomizer.  The resulting array will have the same
     * elements, but arranged into a (possibly) different order.
     *
     * @param xs Array to permute.
     * @param random Randomizer to use for permuation.
     * @param <E> the type of objects in the array being permuted
     */
    public static <E> void permute(E[] xs, Random random) {
        for (int i = xs.length; --i > 0; ) {
            int pos = random.nextInt(i);
            E temp = xs[pos];
            xs[pos] = xs[i];
            xs[i] = temp;
        }
    }

    /**
     * Randomly permutes the elements of the specified integer array
     * using a newly created randomizer.  The resulting array will
     * have the same elements, but arranged into a (possibly)
     * different order.  The randomizer is created with a call to
     * the nullary constructor {@link java.util.Random#Random()}.
     *
     * @param xs Array to permute.
     */
    public static void permute(int[] xs) {
        permute(xs, new Random());
    }

    /**
     * Randomly permutes the elements of the specified integer
     * array using the specified randomizer.
     *
     * @param xs Array to permute.
     * @param random Randomizer to use for permutations.
     */
    public static void permute(int[] xs, Random random) {
        for (int i = xs.length; --i > 0; ) {
            int pos = random.nextInt(i);
            int temp = xs[pos];
            xs[pos] = xs[i];
            xs[i] = temp;
        }
    }


    /**
     * A length <code>0</code> array of integers.
     */
    public static final int[] EMPTY_INT_ARRAY = new int[] { };


}

   
    
  








Related examples in the same category

1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.A random.org seeded random generator.
31.Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.
32.A random choice maker
33.Memory-efficient map of keys to values with list-style random-access semantics.
34.Generates a random integer inside the lo and hi interval
35.Randomizer
36.RandomGUID generates truly random GUIDs
37.A random access text file allows a text file to be accessed randomly starting at any position.
38.A garbage-free randomised quicksort
39.A linear random method based on xor shifts
40.Mersenne Twister Random
41.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
42.generate string consists of random characters.
43.Atomic Pseudo Random
44.Atomic Simple Random
45.Mersenne
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice