【问题标题】:How to stop getting duplicate numbers while using random.range?如何在使用 random.range 时停止获取重复的数字?
【发布时间】:2019-05-09 09:48:15
【问题描述】:

我正在尝试制作数学乘法应用程序。Learninig Unity 和 c#。使用 Random.range 为答案按钮分配一个正确答案和 3 个随机值。但是,有时会将重复值分配给答案按钮。我的意思是为两个或有时三个答案按钮分配了相同的值。我附上了屏幕截图。任何人都可以帮我解决这个问题吗?谢谢

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

}

【问题讨论】:

  • 每次你选择一个随机数时,检查它是否已经被选择(或者它是否匹配正确的答案)。如果它已被选中,则选择一个不同的数字。继续检查和挑选,直到没有重复。只有四个数字(1 个答案,3 个随机),只要您的范围足够宽,这并不难。
  • @rossum 谢谢你的建议。是的,我明白这一点,但不知道如何相互比较随机值。我可以使用哪种方法?
  • 要获得三个唯一的数字,请尝试以下代码: Random rand = new Random(); int[] number = Enumerable.Range(0, 10).Select(x => new { x = x, i = rand.Next() }).OrderBy(x => xi).Select(x => xx) .ToArray(); int number1 = number[0]; int number2 = number[1]; int number3 = number[2];
  • @rossum:最好只创建一个唯一编号的数组然后排序。然后从排序数组中取出前三个数字。
  • @jdweng 对于随机我更喜欢随机排列的数组。对其中三个数字进行排序可能会使正确答案脱颖而出:“1、2、42、3”。我认为更简单的技术可能更容易,并且您仍然需要在改组之前从数组中删除正确的答案。

标签: c# unity3d random duplicates multiplication


【解决方案1】:

一个解决方案的例子:我已经把易于理解的代码放在了统一的地方,而且我保留了你的逻辑..

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


}

使用数组的不同逻辑的相同示例:

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
}

【讨论】:

  • 非常感谢先生。问题已解决。我非常感谢您编写此代码并使我易于理解的努力。
猜你喜欢
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-01-17
  • 2020-02-26
  • 1970-01-01
相关资源
最近更新 更多