Here I have a pseudo-algorimth for Murderer/Sheriff selection:
Let players vote if they want to be a Murderer, Sheriff or Innocent(default)
If 0 Players want to be Murderer/Sheriff, it will be picked randomly as it currently is.
If 1 Player wants to be Murderer/Sheriff, (s)he will be picked.
If 2 or more Players want to be Murderer/Sheriff, it will be picked from them accordingly to how often they've been Murderer/Sheriff. The more often the lower chance.
Example:
5 Players want to be Murderer. And they've been murderer this amount of times
(Player | Count):
Player 1 | 5 times
Player 2 | 3 times
Player 3 | 3 times
Player 4 | 4 times
Player 5 | 2 times
The sum of times they've all been murderer is 5+3+3+4+2 = 17 times.
Player 1 has been Murderer most of times, hence, he has the lowest chance to be picked.
Player 2 and 3 have the same chance to be picked.
Player 4 has low chance to be picked
Player 5 has the highest chance to be picked.
totalChance = sum * (nPlayers - 1) = 17 * (5 - 1) = 17 * 4 = 68
chancePerPlayer = (sum - count) / totalChance
so each player wil have a chance of:
Player1 | (17 - 5) / 68 = 12 / 68 = 17.65%
Player2 | (17 - 3) / 68 = 14 / 68 = 20.59%
Player3 | (17 - 3) / 68 = 14 / 68 = 20.59%
Player4 | (17 - 4) / 68 = 13 / 68 = 19.12%
Player5 | (17 - 2) / 68 = 15 / 68 = 22.06%
Also, for any player that has never been murderer, the formula gets reduced to:
1 / (nPlayers - 1) which in above's example would be 1 / 4 = 25%
Condition: If totalChance = 0 (basically, all players who applied have never been Murderer/Sheriff) then they will be picked randomly from the list of users who applied.
Curious things of this algorimth:
1) If all players except 1 have never been Murderer/Sheriff, the player who has been murderer is basically excluded.
2) This formula won't work if all players have never been Murderer/Sheriff because of Division by Zero. Hence, this condition has to be handled separately
3) Generally, the probabily is very fair with all players, so if there are 2 players and one has been murderer 10 times and the other only 2 times, then the chance is:
Player1 | (12 - 10) / 12 = 2 / 12 = 16.67%
Player2 | (12 - 2) / 10 = 10 / 12 = 83.33%
Another example assuming a player with 12 times, another with 8 times and another with 0:
Player1 | (20 - 12) / 40 = 8 / 40 = 20%
Player2 | (20 - 8) / 40 = 12 / 40 = 30%
Player3 | (20 - 0) / 40 = 20 / 40 = 50%
As you can see, the player that was never a Murderer/Sheriff has 50% of chance, while the others still have a 20 and 30% chance respectively which is good and fair.
Let players vote if they want to be a Murderer, Sheriff or Innocent(default)
If 0 Players want to be Murderer/Sheriff, it will be picked randomly as it currently is.
If 1 Player wants to be Murderer/Sheriff, (s)he will be picked.
If 2 or more Players want to be Murderer/Sheriff, it will be picked from them accordingly to how often they've been Murderer/Sheriff. The more often the lower chance.
Example:
5 Players want to be Murderer. And they've been murderer this amount of times
(Player | Count):
Player 1 | 5 times
Player 2 | 3 times
Player 3 | 3 times
Player 4 | 4 times
Player 5 | 2 times
The sum of times they've all been murderer is 5+3+3+4+2 = 17 times.
Player 1 has been Murderer most of times, hence, he has the lowest chance to be picked.
Player 2 and 3 have the same chance to be picked.
Player 4 has low chance to be picked
Player 5 has the highest chance to be picked.
totalChance = sum * (nPlayers - 1) = 17 * (5 - 1) = 17 * 4 = 68
chancePerPlayer = (sum - count) / totalChance
so each player wil have a chance of:
Player1 | (17 - 5) / 68 = 12 / 68 = 17.65%
Player2 | (17 - 3) / 68 = 14 / 68 = 20.59%
Player3 | (17 - 3) / 68 = 14 / 68 = 20.59%
Player4 | (17 - 4) / 68 = 13 / 68 = 19.12%
Player5 | (17 - 2) / 68 = 15 / 68 = 22.06%
Also, for any player that has never been murderer, the formula gets reduced to:
1 / (nPlayers - 1) which in above's example would be 1 / 4 = 25%
Condition: If totalChance = 0 (basically, all players who applied have never been Murderer/Sheriff) then they will be picked randomly from the list of users who applied.
Curious things of this algorimth:
1) If all players except 1 have never been Murderer/Sheriff, the player who has been murderer is basically excluded.
2) This formula won't work if all players have never been Murderer/Sheriff because of Division by Zero. Hence, this condition has to be handled separately
3) Generally, the probabily is very fair with all players, so if there are 2 players and one has been murderer 10 times and the other only 2 times, then the chance is:
Player1 | (12 - 10) / 12 = 2 / 12 = 16.67%
Player2 | (12 - 2) / 10 = 10 / 12 = 83.33%
Another example assuming a player with 12 times, another with 8 times and another with 0:
Player1 | (20 - 12) / 40 = 8 / 40 = 20%
Player2 | (20 - 8) / 40 = 12 / 40 = 30%
Player3 | (20 - 0) / 40 = 20 / 40 = 50%
As you can see, the player that was never a Murderer/Sheriff has 50% of chance, while the others still have a 20 and 30% chance respectively which is good and fair.