fill an array Randomly - Java Collection Framework

Java examples for Collection Framework:Array Random Value

Description

fill an array Randomly

Demo Code


//package com.java2s;

import java.util.Random;

public class Main {
    /**//from ww w . j  a  v a 2 s  .  c  om
     * @author TheMrMilchmann
     * @since DerpieLang v1.0.0
     */
    public static final <T> T[] fillRandomly(T[] from, T[] into) {
        return fillRandomly(from, into, System.currentTimeMillis());
    }

    /**
     * @author TheMrMilchmann
     * @since DerpieLang v1.0.0
     */
    public static final <T> T[] fillRandomly(T[] from, T[] into, long seed) {
        Random random = new Random(seed);

        for (int i = 0; i < into.length; i++) {
            into[i] = from[random.nextInt(from.length)];
        }

        return into;
    }
}

Related Tutorials