top of page
  • Pinterest
  • Facebook
  • Instagram

BirthdayParadox

  • Writer: Lim Long Teck
    Lim Long Teck
  • Dec 30, 2021
  • 1 min read

The Birthday Paradox asks the question, “If you asked random people their birthday, how many people do you expect to ask before you find two people with the same birthday?”

Write a program that randomly generates integers between 1 and 365 inclusive, one by one. The program ends when two of the generated values match, and prints out the number of tries it took.


Program:

import random

templist = []

while True:

num = random.randint(1,365)

print('{} was randomly generated.'.format(num))

if num not in templist:

templist.append(num)

else:

templist.append(num)

break

print('Duplicate day! This took {} tries.'.format(len(templist)))


Output:

214 was randomly generated.

37 was randomly generated.

117 was randomly generated.

247 was randomly generated.

67 was randomly generated.

300 was randomly generated.

313 was randomly generated.

296 was randomly generated.

354 was randomly generated.

149 was randomly generated.

187 was randomly generated.

184 was randomly generated.

173 was randomly generated.

7 was randomly generated.

46 was randomly generated.

326 was randomly generated.

346 was randomly generated.

173 was randomly generated.

Duplicate day! This took 18 tries.

Recent Posts

See All
CountLetters

Write a Python program to - Prompt user to input a sentence - Count the occurrence of each letter in the input sentence - Save the result...

 
 
 
ChristmasTree

Write a program to prompt the user for a character and a number, and then print a Christmas tree accordingly. Program: char =...

 
 
 
NumberGuessing

Write a program that simulates a number guessing game. It first generates a random number between 1 and 100. It then prompts user to...

 
 
 

Comments


bottom of page