Hello everyone!
I am writing a program for the birthday paradox that runs trials 10,000 times. I want to show that when N people are in a room, there is a 50% chance that there will be a duplicate birthday.
I want to create an empty set, then add to it random birthdays. Then I want to see if there are any duplicates, and if there are, I want to calculate the percentage of sets that have duplicates. I then want to stop the program when N people exceeds 365 or when the percentage hits 50%.

However, I am not sure where my code is going wrong or how to implement the block of code that makes it stop when the target percentage is reached, or when N = 23. Therefore, I have not implemented that aspect.

Could someone tell me if the logic of the code is correct and how to implement it stopping?

  import java.util.HashSet;
    import java.util.Random;

    public class birthdayProblem {
        public static void main(String[] args) {

            //repeat trial for increasing values of N starting at 2
            int yearLength = 365;
            double targetPercentage = 0.5;
            int duplicate = 0;
            int people = 2;
            int i = 0;
            int totalSet = 0;
            while (i <= 10000) {
                for (int j = 0; j <= yearLength; j++) {
                    Random birthdays = new Random();
                    HashSet<Integer> hash = new HashSet<>();

                    for (i = 0; i < 365; i++)
                        hash.add(1 + birthdays.nextInt(365));
                    birthdays.nextInt(365);
                    if (hash.contains(duplicate)) {
                        duplicate++;
                        break;
                    } else {
                        hash.add(duplicate);
                        totalSet++;
                        people++;
                    }

                }
                double average = (double) duplicate / totalSet;
                System.out.println("N value is " + people + " and the percentage is " + average);
            }


        }
    }

This is marked as "solved". Is that right?

Solved? This is how I solve 10 trials and display each time a "paradox" is found.

#include <iostream>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <random>
#include <iomanip>

int para();    
int reset();
double paradox = 0;   
int trial = 1;
int debug = 1;

int main()
{
    reset();
    para();
}

int reset()
{
    int birthdays[4000] = { 0 };
    for (int reset = 0; reset < 4000; reset++)
    {
        birthdays[reset];
    }
    return 0;
}

int next()
{
    para();
    return 0;
}
int para()
{
            reset();
            int birthdays[4000] = { 0 };
            int yearLength = 365+1;
            double targetPercentage = 0.5;
            double rate = 0.0;
            double loops = 1;
            double duplicate = 0;
            double people = 2;
            int i = 0;
            int j = 0;
            int z = 0;

            int counter = 0;
            double dup = 0;
            constexpr int MIN = 1;
            constexpr int MAX = 365;

            std::srand(static_cast<unsigned int>(std::time(nullptr)));
            people = 1;
            rate = 1;

            while (trial <= 10){
                for (int x = 0; x <= 1; x++) {
                    for (int j = 0; people <= 2; j++) {
                        for (i = 2; i <= 365; i++) {
                            int randomNumber = rand() % MAX + MIN;
                            birthdays[i] = randomNumber;

                            for (int check = 0; check <= 365; check++)
                            {
                                if (birthdays[check] == randomNumber)
                                {
                                    duplicate = duplicate + 1;
                                    dup = duplicate - people;
                                }
                            }

                            rate = dup / people; 

                            if (debug == 1)
                            {
                                std::cout << std::setw(5) << randomNumber << std::setw(5);

                                std::cout << std::setprecision(0) << std::fixed;

                                std::cout << birthdays[i] << std::setw(5) << std::setw(5) << people << std::setw(5) << trial << " " << dup << " ";
                                std::cout << std::setprecision(2) << std::fixed;
                                std::cout << rate * 100 << "%" << std::endl;
                            }
                            if (rate >= .5)
                            {
                                std::cout << "Paradox found." << std::endl;;

                                std::cout << "-------------------------------------------------------------------------------" << std::endl;
                                std::cout << std::setw(10) << " Trial#" << std::setw(20) << "People" << std::setw(20) << "Duplicates" << std::setw(20) << " Rate" << std::endl;
                                std::cout << "-------------------------------------------------------------------------------" << std::endl;

                                std::cout << std::setprecision(0) << std::fixed;

                                std::cout <<  std::setw(10) << trial << std::setw(20) << people << std::setw(20) << dup << std::setw(20);
                                std::cout << std::setprecision(2) << std::fixed;
                                std::cout << rate * 100 << "%" << std::endl;
                                std::cout << "-------------------------------------------------------------------------------" << std::endl;
                                paradox++;
                                break;
                            }
                            else {
                                if (debug == 2)
                                {
                                    std::cout << "No Paradox found." << std::endl;
                                }
                            }
                            people++;
                        }
                        if (debug == 1)
                        {
                            std::cout << std::setprecision(0) << std::fixed;
                            std::cout << "Total Paradox: " << paradox << std::endl;
                        }

                    }              
                    if (debug == 2)
                       {
                          std::cout << "Starting next trial..." << std::endl;
                       } 

                       trial++;   
                       next();
                       }
             }    

            std::cout << "Trials done." << std::endl;
            double p_rate = 0.0;
            p_rate = paradox / (trial-1);
            std::cout << " Paradox Total: " << paradox << "\t"<<"Trials: " << trial - 1 << " Rate: " << p_rate * 100 << "%" << std::endl;

            system("pause");

            exit(1);
            return 0;
         }

Paradox found.
-------------------------------------------------------------------------------
    Trial#              People          Duplicates                Rate
-------------------------------------------------------------------------------
         5                 316                 159               50.32%
-------------------------------------------------------------------------------
Total Paradox: 4
Trials done.
 Paradox Total: 4       Trials: 10 Rate: 40%
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.