忽然想起这个命题, 在若干年以前, 军训的时候教官说我们班上40多人里必然有两个人同一天过生日, 还说没看过我们的生日, 感觉跟魔术一样. 让大家验证了一下, 居然有两组人的生日都在同一天. 而在之前公司里面20多个人, 也有同一天过生日的. 那么究竟一组人群里重复生日的概率是多少呢? 想验证一下.
用node.js来验证, 验证10万组:

const testTimes = 100000
const yearLength = 365

function main(){
  var doubledBirthdaysArrayLengths = []
  for(let i = 0; i < testTimes; i++){
    doubledBirthdaysArrayLengths.push(doubledLength())
  }
  console.log("一组人群中有相同生日的概率是多少?")
  console.log("有重复生日的人群的人数: ")
  console.log(doubledBirthdaysArrayLengths)
  console.log('生日重复的一组中最多人数: ', Math.max(...doubledBirthdaysArrayLengths))
  console.log('生日重复的一组中最少人数: ', Math.min(...doubledBirthdaysArrayLengths))
  console.log('生日重复的一组中平均人数: ', average(doubledBirthdaysArrayLengths))
  distribute(doubledBirthdaysArrayLengths)
}
function distribute(arr){
  let peopleNumsofGroups = [] //一组中的人数为数组下标, 数组值为此组人数出现的次数.
  for(let i = 0; i < yearLength; i+=1){
    peopleNumsofGroups.push(0)
  }
  for(let a of arr){
    peopleNumsofGroups[a]++
  }
  // console.log('Distribute: ', days)
  console.log('重复生日人群, 某一人数出现的次数:')
  printArrayInTable(peopleNumsofGroups, 0)
  let probes = [], sum = average(peopleNumsofGroups) * peopleNumsofGroups.length
  for(let i = 0; i < yearLength; i+=1){
    if(i == 0){
      probes[i] = peopleNumsofGroups[i]/sum * 100
    }else{
      probes[i] = peopleNumsofGroups[i]/sum * 100 + probes[i - 1]
    }
  }
  console.log('概率:')
  printArrayInTable(probes, 2)
}
function printArrayInTable(arr, fixDigits){
  console.log('\t[0]\t[1]\t[2]\t[3]\t[4]\t[5]\t[6]\t[7]\t[8]\t[9]')
  for(let row = 0; row < yearLength / 10 + 1; row+=1){
    let out = '[' + row + ']\t' 
    for(let col = 0; col < 10; col+=1){
      let index = row*10 + col
      if(index >= yearLength){
        return
      }
      out += arr[row*10 + col].toFixed(fixDigits) + '\t'
    }
    console.log(out)
  }
}
function average(arr){
  let sum = 0
  for(let a of arr){
    sum += a
  }
  return sum / arr.length
}

function doubledLength(){
  let birthdays = []
  for(let i = 0; i < yearLength + 1; i+=1){
    var birthday = parseInt(Math.random()*yearLength)
    if(findDouble(birthday, birthdays)){
      birthdays.push(birthday)
      // console.log("birthdays: ", birthdays)
      return birthdays.length
    }
    birthdays.push(birthday)
  }
  return birthdays.length
}

function findDouble(obj, arr){
  for(let a of arr){
    if(a == obj){
      return true
    }
  }
  return false
}

main()

输出结果如下:

一组人群中有相同生日的概率是多少?
有重复生日的人群的人数: 
[
  19, 22, 14,  3, 13, 25, 10, 24, 56,  5, 15, 27,
  25, 18, 27, 19, 22, 38, 21, 41,  9, 13, 37, 36,
  28, 25, 22, 29, 11, 23, 19, 14, 13, 16, 32, 26,
   8, 19,  7, 36,  6, 22, 18, 23, 20, 33, 32, 21,
  64, 22, 48, 43,  5, 16, 14,  7, 19, 32, 15, 16,
  15, 33, 58, 19, 26, 18, 45, 31,  4,  5, 30, 26,
  14, 19, 31, 14,  6,  9, 27, 12, 47, 51,  7, 21,
  16, 42, 66, 15, 28, 32, 18, 10, 12, 14, 10, 16,
  23, 15, 23,  8,
  ... 99900 more items
]
生日重复的一组中最多人数:  84
生日重复的一组中最少人数:  2
生日重复的一组中平均人数:  24.60991
重复生日人群, 某一人数出现的次数:
        [0]     [1]     [2]     [3]     [4]     [5]     [6]     [7]     [8]     [9]
[0]     0       0       279     577     842     1006    1334    1520    1873    1949
[1]     2223    2441    2627    2684    2975    2878    3110    3148    3229    3227
[2]     3269    3199    3180    3195    2974    3076    3014    2846    2747    2650
[3]     2624    2440    2327    2111    2096    1863    1807    1647    1564    1399
[4]     1271    1162    1023    996     884     773     738     642     575     541
[5]     460     394     384     325     277     247     203     173     143     148
[6]     111     97      79      71      55      53      41      36      31      18
[7]     24      16      9       16      4       5       7       2       6       6
[8]     1       0       1       0       2       0       0       0       0       0
[9]     0       0       0       0       0       0       0       0       0       0
[10]    0       0       0       0       0       0       0       0       0       0
[11]    0       0       0       0       0       0       0       0       0       0
[12]    0       0       0       0       0       0       0       0       0       0
[13]    0       0       0       0       0       0       0       0       0       0
[14]    0       0       0       0       0       0       0       0       0       0
[15]    0       0       0       0       0       0       0       0       0       0
[16]    0       0       0       0       0       0       0       0       0       0
[17]    0       0       0       0       0       0       0       0       0       0
[18]    0       0       0       0       0       0       0       0       0       0
[19]    0       0       0       0       0       0       0       0       0       0
[20]    0       0       0       0       0       0       0       0       0       0
[21]    0       0       0       0       0       0       0       0       0       0
[22]    0       0       0       0       0       0       0       0       0       0
[23]    0       0       0       0       0       0       0       0       0       0
[24]    0       0       0       0       0       0       0       0       0       0
[25]    0       0       0       0       0       0       0       0       0       0
[26]    0       0       0       0       0       0       0       0       0       0
[27]    0       0       0       0       0       0       0       0       0       0
[28]    0       0       0       0       0       0       0       0       0       0
[29]    0       0       0       0       0       0       0       0       0       0
[30]    0       0       0       0       0       0       0       0       0       0
[31]    0       0       0       0       0       0       0       0       0       0
[32]    0       0       0       0       0       0       0       0       0       0
[33]    0       0       0       0       0       0       0       0       0       0
[34]    0       0       0       0       0       0       0       0       0       0
[35]    0       0       0       0       0       0       0       0       0       0
概率:
        [0]     [1]     [2]     [3]     [4]     [5]     [6]     [7]     [8]     [9]
[0]     0.00    0.00    0.28    0.86    1.70    2.70    4.04    5.56    7.43    9.38
[1]     11.60   14.04   16.67   19.36   22.33   25.21   28.32   31.47   34.70   37.92
[2]     41.19   44.39   47.57   50.77   53.74   56.81   59.83   62.67   65.42   68.07
[3]     70.70   73.14   75.46   77.57   79.67   81.53   83.34   84.99   86.55   87.95
[4]     89.22   90.38   91.41   92.40   93.29   94.06   94.80   95.44   96.01   96.56
[5]     97.02   97.41   97.79   98.12   98.40   98.64   98.85   99.02   99.16   99.31
[6]     99.42   99.52   99.60   99.67   99.72   99.78   99.82   99.85   99.88   99.90
[7]     99.93   99.94   99.95   99.97   99.97   99.98   99.98   99.98   99.99   100.00
[8]     100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[9]     100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[10]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[11]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[12]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[13]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[14]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[15]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[16]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[17]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[18]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[19]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[20]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[21]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[22]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[23]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[24]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[25]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[26]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[27]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[28]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[29]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[30]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[31]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[32]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[33]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[34]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00
[35]    100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00  100.00

可以看到, 班上有40个人的时候, 有重复生日的概率已经高达90%左右了. 按中国班级的规定, 小学标准是45人,有94%的概率有两人同一天过生日. 初高中是50人, 概率则高达97%.如果把两个班放在一起, 概率就是100%了.

标签: none 阅读量: 1528

添加新评论