random Int Array - Java Collection Framework

Java examples for Collection Framework:Array Random Value

Description

random Int Array

Demo Code


//package com.java2s;

import java.util.HashSet;

import java.util.Random;

public class Main {
    public static void main(String[] argv) {
        int size = 42;
        System.out.println(java.util.Arrays.toString(randomIntArray(size)));
    }/*from   ww  w  .  j av  a 2s  . c o m*/

    public static Integer[] randomIntArray(int size) {

        Integer a[] = new Integer[size];
        HashSet<Integer> set = new HashSet<>();
        for (int x = 0; x < a.length; x++) {

            Random r = new Random();
            Integer i = r.nextInt(size * 10);

            while (set.contains(i)) {
                i = r.nextInt(size);
            }
            set.add(i);
            a[x] = i;
        }
        return a;
    }
}

Related Tutorials