vendredi 27 novembre 2015

Java - Generating A Set of Random Numbers Without Duplicates [duplicate]

This question already has an answer here:

With the following code I am attempting to generate 10 random numbers between 1 and 50 without any duplicates being printed out.

My current code is in the file RandomNum.java:

public class RandomNum
{
    public static void main(String[] args) 
    {
             int counter = 0;
             int num = 0;
             while(counter<=10)
             {
                    num=(int)(1+Math.random()*(50));
                    System.out.println("The number"+" "+num+" "+"was drawn.");
                    ++counter;
            }
    }
}

This code successfully generates and prints out the value of the numbers, but I want to make it so that the program prints out 10 unique numbers between 1 and 50 rather than including any duplicates.

How would I go about doing this?

Thanks!




Aucun commentaire:

Enregistrer un commentaire