samedi 27 février 2016

Random number generator to array, with no duplicates java

So I am trying to make a program that produces an array of 20 random numbers, that does not have duplicates (to the end user). Here is my code so far

import java.util.*;
public class randomprog
{
public static void main(String args[])     
{
    Random rand = new Random();
    int[] list = new int[20];
    boolean generating=true;
    int counting=0;
    while(generating)
    {
        int testNum= rand.nextInt(30)+1;
        if (Arrays.asList(list).contains(testNum))
        {}
        else
        {
            list[counting]=testNum;
            counting++;
            System.out.println(testNum);
        }
        if(counting>=20)
        {
            generating=false;
        }
    }
}}

So as you can see I have already tried using Arrays.asList(list).contains(mynumber) however I still recieve duplicates in my output like 29 4 4 1 20 30 20 23 30 11 6 7 27 14 16 8 4 19 7 15

Any suggestions?




Aucun commentaire:

Enregistrer un commentaire