简体   繁体   中英

how to run a while loop several time

I have tring to solve this task, but i can not run while loop more than 1 time.

In this task you will solve what we call the birthday problem. Suppose that we have an empty classroom. students coming into the classroom one by one. We assume that each student has a random birthday between 0 and 365, where 0 represents January 1st and 364 represents last December. The problem consists in finding out the average of the number of students who must enter the classroom so that two students have a birthday on the same day. Tip: run a large number of simulations. For each simulation use an array to compare random birthdays too students.

public class Main {


    public static void main(String[] args) {
        int count;
        boolean[] used;

        used = new boolean[365];

        count = 0;

        while (true) {
            int birthday;  // The selected birthday.
            birthday = (int) (Math.random() * 365);
            count++;
            if (used[birthday]) {
                // This day was found before; It's a duplicate.
                break; }

            used[birthday] = true; }
        System.out.println("A duplicate birthday was found after "
                + count + " kids came to the class.");


      //  double[] arr = {Numbers};
       // double total = 0;

        //for(int i=0; i<arr.length; i++){
           // total = total + arr[i];
        //}



        //double average = total / arr.length;


       // System.out.format("The average is: %", average);

    }

Just surround the assignment and count within a for loop, for the number of simuations.

for (int simulation = 0; simulation < 100000; simulation++) {...... }

And after every break of the while loop, add the count to another variable. Once all your simulations are done, divide the count by the number of simulations to get the average "Birthday Problem" probability.

You can use a for loop around the simulation.

I would suggest putting the simulation code into a separate method to make things clearer. Here's a complete example:

public static void main(String[] args) throws Exception {
    int runs = 100000;
    long studentsNeeded = 0;
    for (int i = 0; i < runs; i++) {
        studentsNeeded += studentsNeededBeforeWeFoundASharedBirthday();
    }
    double meanStudentsNeeded = (double) studentsNeeded / runs;
    System.out.println(String.format("After %d runs, it took %.2f students on "
            + "average before we found a shared birthday.", runs, meanStudentsNeeded));
}

private static int studentsNeededBeforeWeFoundASharedBirthday() {
    Random r = new Random();
    boolean[] used = new boolean[365];
    int count = 0;
    while (true) {
        count++;
        int birthday = r.nextInt(365);
        if (used[birthday]) {
            break;
        }
        used[birthday] = true;
    }
    return count;
}

Prints:

After 100000 runs, it took 24.61 students on average before we found a shared birthday.

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