lundi 8 juin 2015

how to check for duplication of numbers when using the Math.random function

any help please. so, i need to random 6 numbers out of 40. then display them by using bubble sort. i had already written the code but the issue about it, is that sometime i'm having duplicate numbers. what should i do to prevent duplication when having my random numbers display.

public static void main(String[] args) {
    try {

        int numbers[] = new int[6];  //random numbers will be stored in new array
        //2 loop will be created to avoid duplication of numbers 
        System.out.println("Array before Bubble sort");
        for (int i = 0; i < 6; i++) {
            numbers[i] = (int) (Math.random() * 40);

            System.out.print(numbers[i] + ",");  //random numbers will be printed before using sorting bubble sort

        }
        //sort an array using bubble sort
        bubbleSort(numbers);
        System.out.println("    \nArray after bubble sort");
        for (int i = 0; i < 6; i++) {
            System.out.print(numbers[i] + ",");
        }

    } catch (Exception e) {
        System.out.println("error");
    }

}

public static void bubbleSort(int[] numbers) {




Aucun commentaire:

Enregistrer un commentaire