-2

We are making our first 2D mobile game in Unity and we need some help. We coded on Java but is also our first time coding C#.

We are making a card game and we want to give to every player a random number, but (here's the important and difficult part for us) we want each random number not to repeat. For example if we have 5 players and we want to assign a random number from 5 to 10, if number 6 is assigned to player 1 then number 6 cant be assigned to other player.

Here's where we are:

//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;
        }
    }
}

Then with the int we make a switch and show on screen what type of player you are. But clearly we are doing something wrong because the numbers repeat itselfs, even with 0 (we dont know how).

We appreciate every help. Thank you very much! (and sorry for our english)

Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
  • create an array with the numbers you want to deal and then choose a random array entry, Use that array element and remove that element, or swap the array elements a number of times and deal the numbers sequentially – rioV8 Feb 13 '21 at 15:46
  • I think you should do a BINGO game tutorial. Learn both how to generate random cards and how to draw the BINGO balls in random order. – Wyck Feb 13 '21 at 15:57

2 Answers2

0

One solution is you can generate a list that contains valid numbers and then shuffle them.

Code to shuffle a list from 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] + " "); 
    } 
Vibhaas
  • 804
  • 7
  • 8
0

Check the code below :

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

Tell me if it helps or if it does not help.

  • Thank you very much! now it doesnt crash but the value assigned is always 0. Do you know how we can fix it? – Blinked Feb 15 '21 at 21:38
  • I have updated the code. I have changed the Random.range to random.next. Check if it works and inform me. If it too did not work try to output the value of randomnum to check whether it outputs value. Check for both random.range and random.next cases. You are always welcome. – Shafi Sahal Feb 16 '21 at 12:14