JS生成随机数进行循环匹配数组
function RndNum(n) { var rnd = ""; for (var i = 0; i < n; i++) rnd += Math.floor(Math.random() * 10); return rnd; } function start(tip) { let maxCount = RndNum(3); let ary = []; let match = 0; let eachCount = 0; console.group(tip); eachAry(ary); console.groupEnd(); function eachAry(ary) { if (eachCount == maxCount) { console.log("已经是查找最大值了,我们无法继续寻找了"); } else { eachCount++; console.log("\r\n 第:" + eachCount + "次执行"); let num = RndNum(3); console.log("num:" + num); if (ary.length <= 0) { ary.push(num); } else { ary.forEach(function (item, index) { if (item == num) { match++; console.log(ary); console.log("匹配到一条数据,Index:" + index); } }); } console.log("总共匹配到:" + match + "条"); if (match == 0) { ary.push(num); eachAry(ary); } } } }
..我试了好多次,发现数组的第一个随机值有很大的几率会和第二个值相同
做这个目地是为了拿到一些瞎**搞来的数据,例如在第几次执行的时候匹配成功了,
还有匹配成功次数,想象一下..如果有三个随机数连续相同...是多么可怕//