Search code examples
c#unity-game-enginerandomduplicatesmultiplication

How to stop getting duplicate numbers while using random.range?


I am trying to make math multiplication application.Learninig Unity and c#. Using Random.range to assign one correct answer and 3 random values to answer buttons. However, sometimes duplicate values are assigned to answer buttons. I mean same value is assigned to two or sometimes three answer buttons.I have attached screenshot. Can any one please help me to solve this issue?? Thanks

 public class Multplication : MonoBehaviour {                            
       //we make this script instance
 public static Multplication instance;
 //its an enum which we help use to identify the current mode of game 
 public enum MathsType
 {
     multiplication1to10,
     multiplication11to20
 }
 //we make a variable of MathsType
 public MathsType mathsType;
 //2 private floats this are the question values a and b
 private float a, b ;
 //the variable for answer value
 [HideInInspector] public float answer;
 //varible whihc will assign ans to any one of the 4 answer button
 private float locationOfAnswer;
 //ref to the button
 public GameObject[] ansButtons;
 //ref to image symbol so player can know which operation is to be done
 public Image mathSymbolObject;
 //ref to all the symbol sprites whihc will be used in above image
 public Sprite[] mathSymbols;
 //get the tag of button 
 public string tagOfButton;
 //varible to check whihc mode is this
 private int currentMode;
 //ref to text in scene where we will assign a and b values of question
 public Text valueA , valueB;


 void Awake()
 {
     MakeInstance();
 }

 //method whihc make this object instance
 void MakeInstance()
 {
     if (instance == null)
     {
         instance = this;
     }
 }

 //at start we need to do few basic setups
 void Start ()
 {

     //we put the location value in tag of button variable
     tagOfButton = locationOfAnswer.ToString();


     if (GameManager.singleton != null)
     {
         //get whihc mode is selected
         currentMode = GameManager.singleton.currentMode;
     }

     //we call the methods
     CurrentMode();

     MathsProblem();

 }

 //this method keeps the track of mode 
 void CurrentMode()
 {
     if (currentMode == 4)
     {
         //depending on the currentmode value we assign the mode

         mathsType = MathsType.multiplication1to10;
     }
     else if (currentMode == 5)
     {

         mathsType = MathsType.multiplication11to20;
     }


 }

 // Update is called once per frame
 void Update ()
 {
     tagOfButton = locationOfAnswer.ToString();
 }


 //this methode calls the respective method for the respective mode
 public void MathsProblem()
 {

     //switch case is used to assign method
     switch (mathsType)
     {

         case (MathsType.multiplication1to10):

             Multiplication1to10();

             break;

         case (MathsType.multiplication11to20):

             Multiplication11to20();

             break;
     }
 }

 //this methode perform Multiplication process
 void MultiplicationMethod()
 {
     b = Random.Range(0, 10);

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

     answer = a * b;

     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<Text>().text = "" + answer;
         }

         else
         {
             // the below code make sure that all the values assigned to the ans button are within the range

             if (a * b <= 30)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range( 0,  30);
             }
             else if (a * b <= 60 & a * b >= 31)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(28, 61);
             }
             else if (a * b <= 90 & a * b >= 61)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(58, 91);
             }
             else if (a * b <= 120 & a * b >= 91)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(88, 121);
             }
             else if (a * b <= 150 & a * b >= 121)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(118, 151);
             }
             else if (a * b <= 200 & a * b >= 151)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(148, 201);
             }

             while (ansButtons[i].GetComponentInChildren<Text>().text == "" + answer)
             {
                 ansButtons[i].GetComponentInChildren<Text>().text = "" + Random.Range(0, 201);
             }
         }

     }


 }

     void Multiplication1to10()
 {
     a = Random.Range(1, 10);
     MultiplicationMethod();
 }

 void Multiplication11to20()
 {
     a = Random.Range(11, 20);
     MultiplicationMethod();
 }

}


Solution

  • an example of solution : i have put easy code to understand and it seems ok with unity, and i have kept your logic..

    private int a, b;
    [HideInInspector] public int answer;
    //varible whihc will assign ans to any one of the 4 answer button
    private int locationOfAnswer;
    
    //this methode perform Multiplication process
    void MultiplicationMethod()
    {
    
        bool reloop;
        int[] keeprandom = new int[ansButtons.Length];
    
    
        b = Random.Range(0, 10);
    
        locationOfAnswer = Random.Range(0, ansButtons.Length);
    
    
        answer = a * b;
    
        valueA.text = ("" + a).ToString();
        valueB.text = ("" + b).ToString(); ;
    
        mathSymbolObject.sprite = mathSymbols[0];
    
        for (int i = 0; i < ansButtons.Length; i++)
        {
            if (i == locationOfAnswer)
            {
                keeprandom[i] = answer;
                ansButtons[i].GetComponentInChildren<Text>().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 (a * b <= 30)
                    {
                        value = Random.Range(0, 30);
                    }
                    else if (a * b <= 60 & a * b >= 31)
                    {
                        value = Random.Range(28, 61);
                    }
                    else if (a * b <= 90 & a * b >= 61)
                    {
                        value = Random.Range(58, 91);
                    }
                    else if (a * b <= 120 & a * b >= 91)
                    {
                        value = Random.Range(88, 121);
                    }
                    else if (a * b <= 150 & a * b >= 121)
                    {
                        value = Random.Range(118, 151);
                    }
                    else if (a * b <= 200 & a * b >= 151)
                    {
                        value = Random.Range(148, 201);
                    }
    
                    keeprandom[i] = value;
                    if (i == 0)
                    {
                        break;
                    }
                    for (int j = 0; j < i; j++)
                    {
                        if (keeprandom[j] == value || value == answer)
                        {
                            reloop = true;
                            break;
                        }
                    }
    
                } while (reloop);
    
                ansButtons[i].GetComponentInChildren<Text>().text = "" + value;
            }
    
        }//for loop
    
    
    }
    

    same sample with a different logic using an array:

    private int a, b;
    [HideInInspector] public int answer;
    //varible whihc will assign ans to any one of the 4 answer button
    private int locationOfAnswer;
    
    //this methode perform Multiplication process
    void MultiplicationMethod()
    {    
        bool reloop;
        bool[]numbers = new bool[201];
    
        b = Random.Range(0, 10);
    
        locationOfAnswer = Random.Range(0, ansButtons.Length);
    
    
        answer = a * b;     
        numbers[answer] = true;
    
        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<Text>().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 <= 30)
                    {
                        value = Random.Range(0, 30);
                    }
                    else if (answer <= 60 & answer >= 31)
                    {
                        value = Random.Range(28, 61);
                    }
                    else if (answer <= 90 & answer >= 61)
                    {
                        value = Random.Range(58, 91);
                    }
                    else if (answer <= 120 & answer >= 91)
                    {
                        value = Random.Range(88, 121);
                    }
                    else if (answer <= 150 & answer >= 121)
                    {
                        value = Random.Range(118, 151);
                    }
                    else if (answer <= 200 & answer >= 151)
                    {
                        value = Random.Range(148, 201);
                    }
    
    
                    if (numbers[value]) //already select?
                    {
                        reloop = true;
                    }             
    
                } while (reloop);
    
                numbers[value] = true;
                ansButtons[i].GetComponentInChildren<Text>().text = "" + value;
            }
    
        }//for loop
    }