如何使生成的数字永远不一样?

时间:2019-04-15 21:49:45

标签: actionscript-3

我是编程的初学者,我一直在尝试使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;
    }
}

代码中没有错误,其他所有东西都运行良好,只是代码行应该使数字永远不一样只是不起作用,并且在运行了几次代码后我最终得到相同的数字。 while((random1 > 24) && (random1 === answerL1) && (random1 === random2) && (random1 === random3));

谢谢您的帮助! :)

2 个答案:

答案 0 :(得分:3)

您的条件 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;
}

答案 1 :(得分:2)

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

&&运算符坚持认为 all 如果要再次运行,则测试为真。 ||是逻辑或,因此测试的 any 组合将导致循环再次运行。

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