生日悖论的Python实现

题目:如果你的班级中有23个学生,那么其中有两个人生日相同的概率为多大?


#!/usr/bin/env python
# coding=utf-8

import random;

def has_duplicates(t):
    s = t[:]; #make a copy of original list
    s.sort();
    for i in range(len(s)-1):
        if s[i] == s[i+1]:
            return True;
    return False;

def genarate_birthdy(numbers):
    birthday_list = [];
    for i in range(numbers):
        birthday_list.append(random.randint(1,365));
    return birthday_list;

def count_matches(student,samples):
    count = 0;
    t= []
    for i in range(samples):
        t= genarate_birthdy(student);
        if has_duplicates(t):
            count+=1

    return count;

num_students = 23;
numb_simulations= 1000000;

count = count_matches(num_students,numb_simulations);
rate = float(count)/numb_simulations*100

print 'After %d simulations'%numb_simulations;
print 'with %d student'%num_students;
print 'there were %d simulations with at least one match' %count;
print 'rate is %f'%rate


已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页