如何检查先前是否统一调用过相同的随机值?

How to check if same random value was called previously in unity?

我正在计算乘法和,其中 int a 和 int b 都从 random.range(1,11) 获取值。并且有 4 个按钮可以单击正确答案。单击正确答案按钮时,我将再次调用总和创建函数,以便出现下一个总和。我的问题是有时我下次会遇到同样的问题。例如,第一次 2 X 5,当我再次回忆起 sum creator 时,我得到 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();

}

你使用循环算法来实现你在这里想要的。 但是了解循环法可能很耗时。

您可以使用下面附带的代码实现相同的效果。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RandomQuestionGentrator : MonoBehaviour {

    List<int> All_possible_values_of_b = new List<int>();  //List to store all possible value that b can hold;

    void Start ()
    {
        int minRange = 1;
        int MaxRange = 11;

        for (int  i = minRange; i <= MaxRange; i++)    //inserting all possible value of b in list
        {
            All_possible_values_of_b.Add(i);
        }
    }

    void Update ()
    {
        if(Input.GetKeyDown("a"))           //call GenrateNewRandomQuestion when user press correect answer button;
        {
            GenrateNewRandomQuestion();
        }
    }

    void GenrateNewRandomQuestion()
    {
        int a = Random.RandomRange(1, 11);      //a can remain random;

        if(All_possible_values_of_b.Count <= 0)     // changing range when all values of b has been used;
        {
            setnewrange();
        }

        int UsedValueOf_b_InList = Random.Range(0, All_possible_values_of_b.Count);    //accessing remaining values of b in list;

        int b = All_possible_values_of_b[UsedValueOf_b_InList];         //b = some value in list;

        All_possible_values_of_b.RemoveAt(UsedValueOf_b_InList);     //dropping the value of b that is used 

        Debug.Log(a + "x" + b);
    }

    void setnewrange()          //setting new range
    {
        int minRange = 12;
        int MaxRange = 30;
        for (int i = minRange; i <= MaxRange; i++)
        {
            All_possible_values_of_b.Add(i);
        }
    }
}

我从你的代码中了解到 SumCreator() 用于生成下一个问题?在那种情况下,List usedValues = new List();不应在该方法内部分配,每次单击按钮时都会创建一个新列表。

    private List<int> usedValues = new List<int>();
    private bool reloop;
    private bool[] numbers = new bool[301];
    private const int MIN = 1;
    private const int MAX = 11;

    void SumCreator()
    {
        a = Random.Range(1, 11);
        b = GetUniqRandomNumber(MIN,MAX);
        // your logic
    }

      private int GetUniqRandomNumber(int min, int max)
        {
            int num = Random.Range(1, 11);

            if (usedNum.Contains(num))
            {
                num = GetUniqRandomNumber(min, max);
            }
            else {
                usedNum.Add(num);
            }
            if(usedNum.Count == max -1) //  THIS WILL PREVENT entering infinite LOOP - 10  should be yourMax number -1 
            {
                usedNum.Clear();
            }
            return num;
        }

确认这对您有帮助