I was bored yesterday, so I made a simulation code for the birthday question. It's a bit weird crying

Asked 2 years ago, Updated 2 years ago, 23 views

#include <iostream>
#include <cstdlib>
#include <random>

#include "pch.h"

using namespace std;


typedef unsigned long long ULL;

void Create_Random_Number(ULL arr[], int Array_Size, int min = 1, int max = 365) {

    int idx;
    random_device rd;
    mt19937_64 rnd(rd());

    uniform_int_distribution<int> rn(min, max);

    for (idx = 0; idx < Array_Size; idx++) {

        arr[rn(rnd)]++;
    }
}

bool Overlap_Check(ULL arr[]) {

    int cnt, TEMP = 0;
    int idx = 0;

    for (cnt = 0; cnt < 365; cnt++) {

        if (arr[cnt] > 1) {

            idx++;
        }
    }

    cout << TEMP << endl;

    return idx > 0 ? true : false;
}

int main(int argc, char const *argv[]) {

    while (true) {

        int idx, cnt;
        int cnt_TEMP;
        int Ovl_cnt = 0;

        cout << "Number of repetitions of number of people" << endl;
        cin >> idx >> cnt;

        if (idx == 0 || cnt == 0) {

            return EXIT_SUCCESS;
        }

        cout << endl;

        for (cnt_TEMP = 0; cnt_TEMP < cnt; cnt_TEMP++) {

            ULL arr[366];

            Create_Random_Number(arr, idx);

            if (Overlap_Check(arr) == true) {

                Ovl_cnt++;
            }
        }

        cout << "Total number of duplicates" <<Ovl_cnt << endl;
        cout << "Total probability of duplication" << ((double)Ovl_cnt / (double)cnt) * 100 << "%" <<< endl;
    }

    return EXIT_SUCCESS;
}
Total number of duplicates = 1000
Total collision probability = 100%

Here's the result. As a result of testing while adding cout, I think there is a problem with the Overlap_Check function. I don't know what it is.

c++

2022-09-22 12:43

1 Answers

I solved it. Stupidly didn't initialize the sequenceLOL


2022-09-22 12:43

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.