Last active
August 29, 2015 14:15
-
-
Save ojas/04e46114ec8915afd442 to your computer and use it in GitHub Desktop.
Notebooks
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"metadata": { | |
"name": "", | |
"signature": "sha256:0c025d13898e4457f1df2d19f109c610049acaa5b621e1c9aa0135551011f5f1" | |
}, | |
"nbformat": 3, | |
"nbformat_minor": 0, | |
"worksheets": [ | |
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [ | |
"#!/usr/bin/env python\n", | |
"\n", | |
"from random import randint\n", | |
"\n", | |
"def simulate_once():\n", | |
" days_in_year = 366\n", | |
" guest_bdays = []\n", | |
" while True:\n", | |
" new_bday = randint(0, days_in_year)\n", | |
" match = new_bday in guest_bdays\n", | |
" guest_bdays.append(new_bday)\n", | |
" if match:\n", | |
" return len(guest_bdays)\n", | |
"\n", | |
"def main():\n", | |
" n_runs = 100000\n", | |
" n_guests_total = 0\n", | |
" for i in range(0, n_runs):\n", | |
" n_guests_total += simulate_once()\n", | |
" print 'Average number of guests', 1.* n_guests_total/n_runs\n", | |
"\n", | |
"if __name__ == \"__main__\":\n", | |
" main()" | |
], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [ | |
{ | |
"output_type": "stream", | |
"stream": "stdout", | |
"text": [ | |
"Average number of guests 24.71479\n" | |
] | |
} | |
], | |
"prompt_number": 11 | |
}, | |
{ | |
"cell_type": "code", | |
"collapsed": false, | |
"input": [], | |
"language": "python", | |
"metadata": {}, | |
"outputs": [] | |
} | |
], | |
"metadata": {} | |
} | |
] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment