如何检查以前在统一中调用相同的随机值?

问题描述 投票:0回答:2

我正在进行乘法求和,其中int a和int b都从random.range(1,11)获取值。并且有4个按钮可以单击正确答案。当单击正确的答案按钮时,我再次调用sum创建器功能,因此将出现下一个总和。我的问题有时候我下次会得到同样的问题。例如,第一次2 X 5,当我再次召回总和创建者时,我得到2 x 5.我尝试将b的值添加到列表然后比较是否先前使用该值,如果是,则再将其随机化。(我试图控制只有b)的重复但是根据我的理解,当我调用sum creator函数时,我正在为b分配单个值。没有循环,因为我一次只想要一个问题。当我回忆起同样的功能时,它不记得以前使用过哪个值。我该如何解决这个问题?请帮忙。

void SumCreator()
{
    // we do multiplication here
    bool reloop;
    bool[] numbers = new bool[301];

    List<int> usedValues = new List<int>();
    a = Random.Range(1, 11);
    b = Random.Range(1, 11);
    while (usedValues.Contains(b))
    {
        b = Random.Range(1, 11);
    }              

    locationOfAnswer = Random.Range(0, ansButtons.Length);

    answer = a * b;
    numbers[answer] = true;

    if (valueA != null && valueB != null)
    {
        valueA.text = a.ToString();
        valueB.text = b.ToString();
    }
    // mathSymbolObject.sprite = mathSymbols[0];

    for (int i = 0; i < ansButtons.Length; i++)
    {
        if (i == locationOfAnswer)
        {
            ansButtons[i].GetComponentInChildren<TextMeshProUGUI>().text = "" + answer;
        }
        else
        {
            // the below code make sure that all the values assigned to the ans button are within the range

            int value = 0;
            do
            {
                reloop = false;
                if (answer <= 10)
                {
                    value = Random.Range(0, 15);
                }
                else if (answer <= 30 & answer >= 11)
                {
                    value = Random.Range(10, 45);
                }
                else if (answer <= 60 & answer >= 31)
                {
                    value = Random.Range(25, 75);
                }
                else if (answer <= 90 & answer >= 61)
                {
                    value = Random.Range(55, 105);
                }
                else if (answer <= 120 & answer >= 91)
                {
                    value = Random.Range(85, 135);
                }

                if (numbers[value]) //already select?
                {
                    reloop = true;
                }

            } while (reloop);

            numbers[value] = true;
            ansButtons[i].GetComponentInChildren<TextMeshProUGUI>().text = "" + value;
        }

    }//for loop
}

按钮Script.cs

//method whihc help us to identify if player has pressed correct or wrong answer
public void checkTheTextofButton()
{

    if (gameObject.CompareTag( MathsAndAnswerScript.instance.tagOfButton))
    {
       // do something                       
    }
    else
    {                      
       //do something else            
    }

    // if question is answered correct, call the sum creator method to create new question
    MathsAndAnswerScript.instance.SumCreator();

}
c# unity3d random duplicates multiplication
2个回答
© www.soinside.com 2019 - 2024. All rights reserved.