Search code examples
c#unity-game-enginerandomduplicatesmultiplication

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


I am making multiplication sum where int a and int b both are getting values from random.range(1,11). and there are 4 buttons to click correct answer. when correct answer button is clicked i am calling sum creator function again so next sum will appear. My problem is some times i get same question next time. for example first time 2 X 5 and when i recall sum creator again i get 2 x 5. i tried adding value of b to a list then comparing if the value is used earlier if yes then again randomize it.( i am trying to control only duplication of b) But as per my understanding I am assigning single value to b when i call sum creator function. there is no loop because i want only one question at a time. and when i recall the same function it does not remember which value was used previously. How do i solve this? please help.

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
}

Button 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();

}

Solution

  • u use Round Robin algo to achieve what u want here. but understanding round robin can be time-consuming.

    u can achieve the same using the code attached below.

    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);
            }
        }
    }