简体   繁体   中英

Why does the output for my program always start with two consecutive zeros even though I wrote a code that doesn't produce zeros or repeated numbers?

I am trying to create a method that produces lottery numbers with a random number generator. There are two groups of numbers. Group one is supposed to have five different numbers that appear in sorted order, with a range of 1-56. Group two consists of a single number with a range of 1-46. When I run the program, group one always begins two consecutive zeros even though I tried to write the code in a way that doesn't allow group one to have zeros or repeating numbers. At first I thought that the problem must have something to do with the random number generator, so I tried debugging the project in NetBeans. As I stepped through the lines of code, I could see the values assigned to n, which is the variable that holds the numbers produced by the random generator. The values of n were 54, 50, 11, 49, and 28. In the program, the values of n are put into a sorted array. So the output for group one should have been 11, 28, 49, 50, 54, but instead it was 0, 0, 11, 28, 49.

Here is my code:

 public static void MakeTickets(){

    int [] Group1= new int [5];

    int Group2;

    int n;

    Random rand= new Random ();

    for (int j=0 ; j<5; j++){
        //Here, I try to make sure that the range for n is 1-56

        n= rand.nextInt(55)+1;  

        //Here, I try to make sure that a number isn't put into the group one
        //array more than once

        while (Arrays.binarySearch(Group1, n)>=0){
            n= rand.nextInt(55)+1; 
        }


        Group1[j]=n;

        Arrays.sort(Group1);
    }

    Random r= new Random();

    int num= r.nextInt(45)+1;

    Group2=num;

    System.out.print("Here is your ticket: Group One= ");

    for(int number: Group1){
        if (number==Group1[4]){
        System.out.print(number);
        } else {
            System.out.print(number+", ");
        }
    }

    System.out.println(" Group Two= "+Group2);
}

Here is the output:

Here is your ticket: Group One= 0, 0, 33, 45, 50 Group Two= 40

I've tried using ThreadLocalRandom instead, but I still had the same problem. Does anyone know what I am doing wrong? Any and all advice is much appreciated.

The Arrays.sort(Group1); is causing the problem.

I believe the Arrays.sort(Group1); should be placed after the first for loop (after generating the Group1 values).

Currently the values are sorted after adding every value.

Initially the values in the array are 0 0 0 0 0

1st Iteration

After generating the first number

n= rand.nextInt(55)+1; //lets assume the generated value is 43

the, array becomes 43 0 0 0 0

After calling the sort ,

Arrays.sort(Group1);

the array becomes 0 0 0 0 43

2nd Iteration.

After generating the second number

n= rand.nextInt(55)+1; //lets assume the generated value is 22

the, array becomes 0 22 0 0 43

After calling the sort ,

Arrays.sort(Group1);

the array becomes 0 0 0 22 43

3rd Iteration

After generating the third number

n= rand.nextInt(55)+1; //lets assume the generated value is 31

the, array becomes 0 0 31 22 43

After calling the sort ,

Arrays.sort(Group1);

the array becomes 0 0 22 31 43

The 4th and 5th iteration do not change the first two values in the array. This way the first 2 number get stuck as 0s, which explains your result.

You can't use Arrays.binarySearch on an unsorted array. In such case result of this operation is unpredictable.

Sort moves all zeros to the beginning of the array. But you are not rewriting them (cause j is being incremented).

I believe the problem is that the int array is being sorted every time. The int array is initialized with values of 0. By sorting the array every time you are changing where the new random number needs to be inserted. So sometimes you are accidentally overwriting one of the randomly generated numbers instead of the 0's.

Perhaps instead of using BinarySearch which requires the sorting just used contains and get rid of the sorting until all of the random numbers are generated. I don't know the exact syntax since I haven't coded in Java for 3 years, but you could do something like Arrays.asList(Group1).contains(n). So...

for (int j = 0; j < 5; j++) {
    //Here, I try to make sure that the range for n is 1-56

    n= rand.nextInt(55)+1;

    while (Arrays.asList(Group1).contains(n)) {
        n = rand.nextInt(55)+1;
    }

    Group1[j] = n;
}

Arrays.sort(Group1);

你好,据我所知,有零位数字的情况,一般是默认分配器是零分配器,那是你没有初始化这个变量。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM