Question by 
               SaritaW · Dec 07, 2018 at 09:25 AM · 
                randommathrandom.rangerandomization  
              
 
              How to stop getting duplicate numbers while using random.range
I am trying to make math multiplication application for learning unity and c#. Referencing free pack from unity asset store "Math Challenge". 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
 using UnityEngine;
 using UnityEngine.UI;
 
 
 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();
     }
 
 }
  
 
               
                 
                error.jpg 
                (16.2 kB) 
               
 
              
               Comment
              
 
               
               
               Best Answer 
              
 
              Answer by elenzil · Dec 07, 2018 at 06:36 PM
 using System.Collections.Generic;
 
 class MainClass
 {
   public static void Main(string[] args)
   {
     RandNoRepeat(5, 20);
   }
   
   public static void RandNoRepeat(int min, int max) {
     List<int> vals = new List<int>();
     for (int n = min; n <= max; ++n) {
       vals.Add(n);
     }
     
     while (vals.Count > 0) {
       int index = new System.Random().Next(0, vals.Count);
       System.Console.WriteLine("-->" + vals[index]);
       vals.RemoveAt(index);
     }
   }
 }
 
 
              Your answer