【问题标题】:How to generate a random number every time and didn't repeated [duplicate]如何每次都生成一个随机数并且不重复[重复]
【发布时间】:2021-02-13 15:37:12
【问题描述】:

我们正在 Unity 中制作我们的第一个 2D 手机游戏,我们需要一些帮助。我们使用 Java 编写代码,但也是我们第一次编写 C#。

我们正在制作一款纸牌游戏,我们希望给每个玩家一个随机数,但是(这对我们来说是重要且困难的部分)我们希望每个随机数不要重复。 例如,如果我们有 5 名玩家,我们想分配一个从 5 到 10 的随机数,如果将 6 号分配给 1 号玩家,则不能将 6 号分配给其他玩家。

我们在这里:

//Player class only have string name and int trabajo
public static void assignRandom(Player jug)
{
    int randomnum;
    bool aleatorio;
    do
    {
        randomnum= Random.Range(1, 5);
        aleatorio = comprobarAleatorio(randomnum);
    }
    while (aleatorio);

    jug.setTrabajo(randomnum);
}

public static bool comprobarAleatorio(int numaleatorio)
{
    bool exists = false;
    int longitud = jugadoresList.Count;

    for (int i = 0; i < 5; i++)
    {
        if (jugadoresList[i].getTrabajo().Equals(numaleatorio))
        {
            exists = true;
        }
    }
}

然后我们使用 int 进行切换并在屏幕上显示您是哪种类型的玩家。但显然我们做错了什么,因为数字会重复,即使是 0(我们不知道如何)。

我们感谢您的每一次帮助。非常感谢! (对不起我们的英语)

【问题讨论】:

  • 用你要处理的数字创建一个数组,然后选择一个随机数组条目,使用该数组元素并删除该元素,或者多次交换数组元素并按顺序处理数字
  • 我认为你应该做一个 BINGO 游戏教程。了解如何生成随机卡片以及如何按随机顺序绘制 BINGO 球。

标签: c# unity3d


【解决方案1】:

一种解决方案是您可以生成一个包含有效数字的列表,然后是shuffle them

here 打乱列表的代码:

// A Function to generate a 
// random permutation of arr[] 
    static void randomize(int []arr, int n) 
    { 
        // Creating a object 
        // for Random class 
        Random r = new Random(); 
          
        // Start from the last element and 
        // swap one by one. We don't need to 
        // run for the first element  
        // that's why i > 0 
        for (int i = n - 1; i > 0; i--)  
        { 
              
            // Pick a random index 
            // from 0 to i 
            int j = r.Next(0, i+1); 
              
            // Swap arr[i] with the 
            // element at random index 
            int temp = arr[i]; 
            arr[i] = arr[j]; 
            arr[j] = temp; 
        } 
        // Prints the random array 
        for (int i = 0; i < n; i++) 
        Console.Write(arr[i] + " "); 
    } 

【讨论】:

    【解决方案2】:

    检查下面的代码:

    public static void assignRandom(Player jug) //Player class only have string name and int trabajo
        {
            int randomnum;
            private Random random = new Random(); //Declared an instance of Random
            ArrayList randomNumsList = new ArrayList();  //Create arraylist to add random nums
            bool aleatorio;
            do
            {
                randomnum= random.next(1, 5); //Changed Random.ranged to random.next();
                randomNumsList.add(randomnum); //add the random num to list
            
                aleatorio = comprobarAleatorio(randomnum);          
            }
            while (aleatorio);
    
            if (!randomNumsList.Contains(randomnum) {   //Only call setTrabajo if the randomNumsList don't have currently generated randomnum
              jug.setTrabajo(randomnum);
            }
        }
    
        public static bool comprobarAleatorio(int numaleatorio)
        {
    
            bool exists = false;
            int longitud = jugadoresList.Count;
    
            for (int i = 0; i < 5; i++)
            {
                    if (jugadoresList[i].getTrabajo().Equals(numaleatorio))
                    {             
                        exists = true;             
                    }
            }
            return exists;
        }
    }
    

    告诉我它是否有帮助或没有帮助。

    【讨论】:

    • 非常感谢!现在它没有崩溃,但分配的值始终为 0。你知道我们该如何解决它吗?
    • 我已经更新了代码。我已将 Random.range 更改为 random.next。检查它是否有效并通知我。如果它也不起作用,请尝试输出 randomnum 的值以检查它是否输出值。检查 random.range 和 random.next 情况。随时欢迎您。
    猜你喜欢
    • 1970-01-01
    • 2019-02-10
    • 1970-01-01
    • 1970-01-01
    • 2017-07-25
    • 2012-05-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多