{ "cells": [ { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "# What is the probability of having the same PID twice in a day?\n", "\n", "The birthday problem\n", "https://www.probabilisticworld.com/birthday-problem-python-simulation/\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from random import randint\n", " \n", " \n", "NUM_PEOPLE = 23\n", "NUM_POSSIBLE_BIRTHDAYS = 365\n", "NUM_TRIALS = 10000\n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ " \n", "def generate_random_birthday():\n", " birthday = randint(1, NUM_POSSIBLE_BIRTHDAYS)\n", " return birthday\n", " \n", " \n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "def generate_k_birthdays(k):\n", " birthdays = [generate_random_birthday() for _ in range(k)]\n", " return birthdays\n", " \n", " \n", "def aloc(birthdays):\n", " unique_birthdays = set(birthdays)\n", " \n", " num_birthdays = len(birthdays)\n", " num_unique_birthdays = len(unique_birthdays)\n", " has_coincidence = (num_birthdays != num_unique_birthdays)\n", " \n", " return has_coincidence\n", " \n", " \n", "def estimate_p_aloc():\n", " num_aloc = 0\n", " for _ in range(NUM_TRIALS):\n", " birthdays = generate_k_birthdays(NUM_PEOPLE)\n", " has_coincidence = aloc(birthdays)\n", " if has_coincidence:\n", " num_aloc += 1\n", " \n", " p_aloc = num_aloc / NUM_TRIALS\n", " return p_aloc\n", " \n", " \n", "p_aloc = estimate_p_aloc()\n", "print(f\"Estimated P(ALOC) after {NUM_TRIALS} trials: {p_aloc}\")\n" ] }, { "attachments": {}, "cell_type": "markdown", "metadata": {}, "source": [ "So the linux PID pool in 32,768 and I have restarted my process 200 times today.\n", "\n", "$$\n", "P(ALOC) + P(NC) = 1\n", "$$\n", "\n", "$$\n", "P(ALOC|k) = 1 - ( 32,768! / (32,768**200 * (365 - 200 )) ) = 0.46\n", "$$\n", "\n", "[ALOC] = at least one coincidence\n", "[NC] = No Coincidence\n", "\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "NUM_PEOPLE = 200\n", "NUM_POSSIBLE_BIRTHDAYS = 32768\n", "NUM_TRIALS = 10000\n", "p_aloc = estimate_p_aloc()\n", "print(f\"Estimated P(ALOC) after {NUM_TRIALS} trials: {p_aloc}\")" ] } ], "metadata": { "language_info": { "name": "python" }, "orig_nbformat": 4 }, "nbformat": 4, "nbformat_minor": 2 }