azl397985856/leetcode

【每日一题】- 2020-06-05 - 10个小球,随机分到12个盒子里,求恰好10个盒子都为空的概率。

azl397985856 opened this issue · 2 comments

10个小球,随机分到12个盒子里,求恰好10个盒子都为空的概率。要求用程序模拟十万次,暴力求出该概率

来自:字节跳动 算法工程师一面的第一题 (3月30日,60分钟,牛客网视频面)https://www.nowcoder.com/discuss/395924

不知道对不对 感觉不能用random 毕竟伪随机

      function random() {
        let count = 10;
        let carry = false;
        let obj = {};
        while (!carry && count) {
          let num = Math.floor(Math.random() * 12);
          if (obj[num]) carry = true;
          obj[num] = 1;
          count--;
        }
        return !carry;
      }
      function Probability() {
        let isTen = 0;
        for (let i = 0; i < 100000; i++) {
          if (random()) isTen++;
        }
        return isTen / 100000;
      }
      console.log(Probability());
      //0.00429
      //0.00396
      //0.00383
stale commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.