# Assign role randomly from array for my online game

hello I just started developing games. I have an idea, but I don’t know how.
. First there will be 3 roles, these are x, y, z. for example (10 people in the game, 2 of them will be x, 1 of them will be y, 7 of them will be z) how to randomly assign these roles to online players, and in the game at night when the options will appear on the screen and only x’s will make a choice and kill someone after 10 seconds and y will make the selection if the person chosen by x also chooses y, the person z or y selected by x will not die, then this will be repeated 2-3 times

There must be a better way to do this but here is what i would do:

I would have a class for every player that would hold the role and an index for later:

``````public class Player
{
public int index;
public char role;
}
``````

For the script that handles the role shuffling i would have an array of all the players we want to suffle:

``````Player[] players = new Player;
``````

You would probaply assing thease players differently but for the sake of this post im going to just do it like this:

``````for (int i =0; i<players.Length;i++)
{
players *= new Player();*
``````

players*.index = i;*
}
Now every time you want to suffle the roles you can make use of the Knuth shuffle algorithm :
void Suffle(Player[] arr)
{
// Knuth shuffle algorithm
for (int t = 0; t < arr.Length; t++)
{
Player tmp = arr[t];
int r = Random.Range(t, arr.Length);
arr[t] = arr[r];
arr[r] = tmp;
}
}
After you have suffeled the indexes you can just assing the roles with a simple for loop:
for(int i = 0; i<players.Length;i++)
{
if(i<2)
players*.role = ‘x’;*
if (i == 2)
players*.role = ‘y’;*
if (i > 2)
players*.role = ‘z’;*
}
here is the whole code:
public class Player //
{ //
public int index; //example player class
public char role; //
} //
public class randomRoles : MonoBehaviour
{
Player[] players = new Player;
void Start()
{

for (int i =0; i<players.Length;i++) //
{ //
players = new Player(); // example assingment
players*.index = i; //*
} //
Suffle(players); // shuffles the order of players in the array
for(int i = 0; i<players.Length;i++) //goes through every player and assings the role
{
if(i<2)
players*.role = ‘x’;*
if (i == 2)
players*.role = ‘y’;*
if (i > 2)
players*.role = ‘z’;*
}
}
void Suffle(Player[] arr) // Knuth shuffle algorithm
{
for (int t = 0; t < arr.Length; t++)
{
Player tmp = arr[t];
int r = Random.Range(t, arr.Length);
arr[t] = arr[r];
arr[r] = tmp;
}
}
}

can you tell me everything from the beginning