Last active
October 14, 2021 14:09
-
-
Save qinlili23333/d682555ac132b83a823638ae65cabb5f to your computer and use it in GitHub Desktop.
同一天生日
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
function getBirthday() { | |
return Math.floor(Math.random() * 366) | |
} | |
function getBirthdayArray(num) { | |
var array = [] | |
for (var i = 0; i < num; i++) { | |
array[i] = getBirthday() | |
} | |
return array | |
} | |
function isArrayRepeat3(array) { | |
var isOver3 = false; | |
var data = array.reduce(function (prev, next) { | |
prev[next] = (prev[next] + 1) || 1; | |
if (prev[next] >= 3) { isOver3 = true; } | |
return prev; | |
}, {}); | |
return isOver3 | |
} | |
function doVerify(people, repeat) { | |
var yes = 0 | |
for (var i = 0; i < repeat; i++) { | |
if (isArrayRepeat3(getBirthdayArray(people))) { | |
yes++ | |
} | |
} | |
return Number(yes) / Number(repeat) | |
} | |
function autoVerify(accuracy) { | |
var stopVerify = false | |
for (people = 3; !stopVerify; people++) { | |
var percent = doVerify(people, accuracy) | |
console.log("people:"+people+", percent:"+percent) | |
if (percent > 0.5) { | |
stopVerify = true | |
return people | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment