如何使生成的数字永远不相同?

How can I make the generated numbers never to be the same?

我是编程初学者,我一直在尝试让我的 3 个随机生成的数字和答案永远不相同,但无论我如何尝试,我都没有得到我想要的结果。如果有人能让我走上正确的道路,我将不胜感激:)

这是第一个随机生成数字的一段代码,其他两个完全一样。

//Random number 1
{
    btnAns1.label = "" + random1;
    if(mathOperationL1 == 1)
    {
        random1 = Math.floor(Math.random()* 24) + 1;
        do
        {
            random1 = Math.floor(Math.random()* 24) + 1;
        }
        while((random1 > 24) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
        btnAns1.label = "" + random1;
    }
    else if (mathOperationL1 == 2)
    {
        random1 = Math.floor(Math.random()* 11) + 1;
        do
        {
            random1 = Math.floor(Math.random()* 11) + 1;
        }
        while((random1 > 11) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
        btnAns1.label = "" + random1;
    }
    else if (mathOperationL1 == 3)
    {
        random1 = Math.floor(Math.random()* 144) + 1;
        do
        {
            random1 = Math.floor(Math.random()* 144) + 1;
        }
        while((random1 > 144) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
        btnAns1.label = "" + random1;
    }
    else if (mathOperationL1 == 4)
    {
        random1 = Math.floor(Math.random()* 12) + 1;
        do
        {
            random1 = Math.floor(Math.random()* 12) + 1;
        }
        while((random1 > 12) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));
        btnAns1.label = "" + random1;
    }
}

代码中没有错误,其他一切正常 perfectly.It 只是应该使数字永远不相同的代码行不起作用,在 [=21 之后=] 几次代码​​我最终得到相同的数字。 while((random1 > 24) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));

提前感谢您的帮助! :)

while((random1 > 24) || (random1 === answerL1) || (random1 === random2) || (random1 === random3));

&& 运算符坚持认为 所有 测试都是正确的,如果它再次 运行 。 || 是逻辑或,因此测试的 任何 组合将导致循环再次进入 运行。

编辑:我还应该说,这段代码中似乎有很多重复的工作。也许一旦您对操作员的行为感到满意,您就可以简化流程。

你的条件 while((random1 > 24)... 总是 false 因为你生成了 1 到 24 这样的数字,它们从不超过 24。

让我们用算法来做。

var aList:Array = new Array;

// Put the user's input here. Don't forget that
// TextField.text contains String value rather than int.
aList[0] = 5;

// Each line adds a random element different from
// the elements that are already in the Array.
aList[1] = smartRandom(1, 10, aList);
aList[2] = smartRandom(4, 15, aList);
aList[3] = smartRandom(9, 20, aList);

// Let's see what we get this time.
trace(aList);

// Generates a random int from "min" to "max" inclusive,
// while avoiding all the numbers from the "avoid" Array.
// Note that it doesn't actually checks if it is possible
// to do so, thus smartRandom(1, 1, [1]) will result in the
// infinite loop because conditions will never be satisfied.
function smartRandom(min:int, max:int, avoid:Array):int
{
    var result:int;

    do
    {
        result = min + Math.random() * (max - min + 1);
    }
    // The Array.indexOf(...) method returns -1 if the argument
    // is not on the given Array or it returns a 0-based
    // index of the element that is equal to the given argument.
    while (avoid.indexOf(result) > -1);

    return result;
}