Javascript随机数生成器遗漏值(仅在有时)

时间:2017-02-09 20:53:55

标签: javascript random

我正在编写一个需要能够生成锦标赛时间表和括号的网络应用。在设置初始池游戏时,我当前的实现将在50-75%的时间内正常工作。但偶尔它会陷入无限循环,因为它永远不会随机选择所需的最后一粒种子。

e.g。如果剩下的唯一一支球队是需要被安排到最后一场比赛中作为对手的第5队,那么随机数生成器会给我除了种子5以外的所有可能的选项,因为某种原因这将是4的种子。

此时有些代码在试图查明我的错误来源时显然已经被混淆了但是到目前为止它已经无效,所以我来到这里。

代码:

teamPoolScheduled = [];
index = 0;
while (index < tournyTeams.length) {
    teamPoolScheduled[index] = [tournyTeams[index], 0];
    index++;
}
gamesScheduled = [];
var randomSeed = 0;
index = 0;
index2 = 0;
var index3 = 0;
var tempTeam1 = "";
var tempTeam2 = "";
var shouldAdd = true;
var index4 = 0;
var breakLoop = true;
while (index < parseInt(numPoolGames.innerHTML)) {
    randomSeed = Math.floor(Math.random() * tournyTeams.length);
    if (gamesScheduled[index] !== undefined && gamesScheduled[index].indexOf(tournyTeams[randomSeed]) >= 0) {
        continue;
    } else {
        if (teamPoolScheduled[randomSeed][1] < tournyPoolGames.value) {
            console.log("---------also here--------");
            if (gamesScheduled[index] === undefined) {
                gamesScheduled[index] = [];
            } else {
                index3 = 0;
                tempTeam1 = gamesScheduled[index][0];
                tempTeam2 = tournyTeams[randomSeed];
                shouldAdd = true;
                while (index3 < gamesScheduled.length) {
                    if ((gamesScheduled[index3][0] == tempTeam1 && gamesScheduled[index3][1] == tempTeam2) || (gamesScheduled[index3][1] == tempTeam1 && gamesScheduled[index3][0] == tempTeam2)) {
                        shouldAdd = false;
                        break;
                    }
                    index3++;
                }
                if (!shouldAdd) {
                    continue;
                }
            }
            if (shouldAdd) {
                gamesScheduled[index][index2] = tournyTeams[randomSeed];
                teamPoolScheduled[randomSeed][1] += 1;
                if (index2 == 1) {
                    index2 = 0;
                    index++;
                } else {
                    index2++;
                }
            }
        } else {
            console.log("Here seed: " + randomSeed);
            index4 = 0;
            breakLoop = true;
            while (index4 < teamPoolScheduled.length) {
                if (teamPoolScheduled[index4][1] != tournyPoolGames.value) {
                    breakLoop = false;
                    console.log(teamPoolScheduled[index4] + " index4: " + index4);
                    break;
                }
                index4++;
            }
            if (breakLoop) {
                break;
            }
        }
    }
}

正确执行的控制台日志看起来像这样

---------also here--------
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team1,2 index4: 0
4newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team2,2 index4: 1
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team2,2 index4: 1
2newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team4,1 index4: 3
newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team4,1 index4: 3
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team4,1 index4: 3
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team4,1 index4: 3
newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team4,2 index4: 3
newTournamentScheduleController.js:135 ---------also here--------
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team7,2 index4: 6
newTournamentScheduleController.js:165 Here seed: 4
newTournamentScheduleController.js:171 team7,2 index4: 6
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team7,2 index4: 6
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team7,2 index4: 6
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team7,2 index4: 6
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team7,2 index4: 6
newTournamentScheduleController.js:135 ---------also here--------

并且无限循环控制台日志的一部分看起来像这样,成千上万行以相同的方式继续向前,并且永远不会在--------这里------日志消息。< / p>

Here seed: 9
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 6
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 6
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 3
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 3
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 3
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 6
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 0
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 7
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 6
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 5
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 6
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 1
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 8
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 9
newTournamentScheduleController.js:171 team5,2 index4: 4
newTournamentScheduleController.js:165 Here seed: 2
newTournamentScheduleController.js:171 team5,2 index4: 4

我的语法或逻辑中的错误或javascript math.random函数的已知问题的任何帮助将不胜感激。请问我是否可以提供任何可能有益的其他细节。

0 个答案:

没有答案