Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
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
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NoDumbQuestion · Dec 07, 2018 at 10:19 AM 0
Share

do some google https://answers.unity.com/questions/498289/not-repeat-random-number-randomrange.html

1 Reply

· Add your reply
  • Sort: 
avatar image
0
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);
     }
   }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image SaritaW · Dec 08, 2018 at 05:18 PM 0
Share

@elenzil thank you very much for your help.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

169 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Random Scenes Without Repetition 1 Answer

Randomly generate blocks on a flat map 0 Answers

Use all variables from array (in Random.Range()) 2 Answers

Need help with specifics in Random.Range 1 Answer

Best way to create an event occurring at a random time? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges