Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8015885
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:23:02+00:00 2026-06-04T20:23:02+00:00

I have a mathematical problem in my JavaScript code. I need to divide a

  • 0

I have a mathematical problem in my JavaScript code. I need to divide a given number of players into 2 teams randomly so that each time – if players want to play again – the teams are formed again and they should be different until all the combinations are formed.

Let’s say I have 4 players, so all the combinations are as follows:
[1,2],[1,3],[1,4],[2,3],[2,4],[3,4]

However, because the team side doesn’t count, there are only 3 different combinations:

[1,2] vs [3,4]
[1,3] vs [2,4]
[1,4] vs [2,3]

When the number of games played exceeds the number of combinations, it should start all over again… i.e. randomly selecting one of the three combination, randomly selecting next one and so on…

But there’s a twist… and mathematical skills of mine go pretty hard south when the number of players is odd, and one of the player needs to rest one game. So, with 5 players all the match-up combinations are (last number being the player resting):

[1,2] vs [3,4] [5]
[1,2] vs [3,5] [4]
[1,2] vs [4,5] [3]

[1,3] vs [2,4] [5]
[1,3] vs [2,5] [4]
[1,3] vs [4,5] [2]

[1,4] vs [2,3] [5]
[1,4] vs [2,5] [3]
[1,4] vs [3,5] [2]

[1,5] vs [2,3] [4]
[1,5] vs [2,4] [3]
[1,5] vs [3,4] [2]

[2,3] vs [4,5] [1]
[2,4] vs [3,5] [1]
[2,5] vs [3,4] [1]

How is it possible in JavaScript to get those teams formed?

One thing that came into mind was to give each player an unique value (10^x), e.g.:

player1.value = 10;
player2.value = 100;
player3.value = 1000;
player4.value = 10000;

…and then when looping to form teams check if one the team’s total value is equal to last values.

Could someone mathematically/JavaScriptly more talented please help me with this coding problem. Thanks!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T20:23:04+00:00Added an answer on June 4, 2026 at 8:23 pm

    Using values is a good idea, but if you make them bit masks it’s easier to check which players s have been matched.
    e.g.

    player1.value = 1 
    player2.value = 2
    player3.value = 4
    //(dynamically player n would have value 1 << (n-1) )
    

    By checking the mask with another player, it can be checked if they have been coupled already, since they already have their own mask value, they can never match themselves.
    As for the random factor, I think the easiest way is to create all combinations first, as you did in your example, and use an array of those combinations as the base of picking a random match.
    If you feel this approach is a good way to go, but have trouble implementing, I can try and cook up some example code.

    Edit here’s the example code, hope the comments help sort out their meaning
    Edit2 changed team combinations

        var playercount = 5; 
    
        //add players
        var players = new Array();
        for (var i = 0; i < playercount; i++) {
            players[i] = { Index: i, Mask: 1 << i, Name: "Player" + (i + 1), toString: function () { return this.Name; } };
            //about the 1 << i:  "<<" is a so called bit wise shift to the left.
            //1 << i has the same outcome as 2 to the power of i
        }
    
        //the line below would print all players
        //for (var pi in players) { var p = players[pi]; document.write(p + " (Mask:" + p.Mask + ")<br>"); } document.writeln("<br>"); 
    
        //create all possible team combinations
        var teams = new Array();
    
        var playersPerTeam = Math.floor(playercount / 2);
        function Team(){
            this.list = new Array();
            this.mask = 0;
            this.count = 0;
            this.full  =false;
    
            this.Add = function (i) {
                this.list.push(players[i]);
                this.mask |= players[i].Mask;
                this.full = ++this.count === playersPerTeam;
            }
    
            this.toString = function () {
                var res = "", cnt = this.list.length;
                for (var i = 0; i < cnt; i++) {
                    if (i > 0)
                        res += i == cnt - 1 ? " and " : ",";
                    res += this.list[i].Name;
                }
                return res;
            }
        }
    
    
        function addplayers() {
            var indices = new Array(playersPerTeam);
            for (var p = 0; p < playersPerTeam; p++) indices[p] = p;
            var l = playersPerTeam - 1;
    
            function addteam() {
                var team = new Team();
                for (var p = 0; p < playersPerTeam; p++) team.Add(indices[p]);
                teams.push(team);
            }
    
            function addplayer(start, depth) {
                var target = players.length - playersPerTeam + depth + 1;
                var team;
                for (var i = start; i < target; i++) {
                    indices[depth] = i;
                    if (depth == l)
                        addteam();
                    else
                        addplayer(i + 1, depth + 1);
                }
            }
    
            addplayer(0, 0);
    
        }
        addplayers();
    
    
    
    
        //the line below would print the team combinations
        //for (var te in teams) { var t = teams[te]; document.write(t + " (mask:" + t.mask + ")<br>"); } document.writeln("<br>");
    
        //create matches
        var matches = new Array();
        //the matches can be created in the same way as the teams, only the first team cannot have players of the second team
        for (var i = 0; i < teams.length; i++) {
            for (var j = i + 1; j < teams.length; j++) {
                var t1 = teams[i], t2 = teams[j];
                if ((t1.mask & t2.mask) === 0) //this is where the masks come in, 
                    matches.push({ Team1: t1, Team2: t2, toString: function () { return this.Team1 + " versus " + this.Team2 } });
            }
        }
    
        //randomize matches. Instead of picking a random match per turn, we can randomize at the
        //start, so we know all the matches in advance.
        //this can be done by using a sort on the array with a random index
        //NB, this isn't the most random randomness (or whatever you call it LOL). For better shuffling
        //there are several alternatives, but perhaps this one is enough
        matches.sort(function() { return (parseInt(Math.random() * 100) % 2);});
    
    
        //the line below prints the matches
        for (var mi in matches) { document.write(matches[mi] + "<br>"); } document.writeln("<br>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a javascript function that accepts a number and performs a mathematical operation
I have a rather mathematical problem I need to solve: The task is to
i have a mathematical problem: i have a function where the only parameter is
I have a large mathematical expression that has to be created dynamically. For example,
I have a problem with LaTeX – I don't know how to put mathematical
I am very new to Fluent NHibernate and I have a problem that I
My problem reduces to finding the number of primes between two given numbers.I could
I have an application that loads lots of data into memory (this is because
i have mathematical problem which I'm unable to convert to PHP or explain differently.
The postage stamp problem is a mathematical riddle that asks what is the smallest

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.