Maths - Remainders - Why use for player start?

I’m pretty sure I understand the concept here. I’ve written a simple Unity C# script to implement it with a user defined number of players. My question is why would you do this instead of just generating a random number between, 0 and 3 for 4 players?

using System.Collections.Generic;
using UnityEngine;

public class WhoGoesFirstUsingRemainder : MonoBehaviour
{
    [SerializeField] int numberOfPlayers;


    void Start()
    {
        Debug.Log(WhoGoesFirst(GeneratePlayers()) + " goes first!");
    }

    private string WhoGoesFirst(List<string> _players)
    {
        System.Random rnd = new System.Random();
        int rndNumber = rnd.Next(numberOfPlayers, 1001);
        int remainder = rndNumber % numberOfPlayers;
        Debug.Log(numberOfPlayers + " Players, using " + rndNumber + " to find the remainder");
        return _players[remainder];
    }

    private List<string> GeneratePlayers()
    {
        List<string> players = new List<string>();
        int index = 0;
        while (index < numberOfPlayers)
        {
            index++;
            players.Add("Player" + index);
        }
        return players;
    }
}

Great work coding up a solution @TotallyFutile.
This is just one illustrative example for using the remainder and not necessarily the only way to solve this particular problem. Remainders have a variety of uses, so think of them as just another tool in your tool belt.

Thanks, Gary. I’m still fairly new to programming in general and I’m aware computers cannot be truly random and wondered if this was somehow connected. Maybe this is somehow “more random”, if that makes sense. :smiley:

Although looking at my code example it has the possibility of being less random depending on the number players as you can have a very slightly more chance of certain remainders. The random line should probably be something like:

int rndNumber = rnd.Next(numberOfPlayers, (numberOfPlayers * 1000) + 1)

Looking at your code, it looks like you’er using rnd.Next wrong/strangely.
The way you have it, it will chose a number between numberOfPlayers and your max value.
If you omit those two parameters and just have rnd.Next(), this will chose any random integer.

So your two main options are rnd.Next(0, numberOfPlayers) if you want to limit the random selection vs rnd.Next() % numberOfPlayers if you want to use the remainder to reduce the answer after a random number has been generated.
Neither option is any more random than the other, so you can use whichever option you like on that front.

Where remainders really come in useful as a programmer is for iterative counters and for breaking up lists into rows/columns.

As an example of the latter, imagine your storing the data for a grid of nodes (maybe you’re doing some pathfinding).
This grid is 5x5 so you have an array of 25 elements.
Now the question is, what are the coordinates of element 17?
Well, the column will be 17 % 5 = 2, and the row will be ⌊17/5⌋ = 3.
So element 17 should be at the coordinates (2, 3).

1 Like

I understand, thank you for taking the extra time!

1 Like

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.

Privacy & Terms