Kunak
Kunak

Reputation: 37

Powershell random team generator with CSV file

I'm working with a script that should import my csv file, and randomly put 3 persons in a team, and make as many teams as there are players.

I can import my csv file, but i'm very lost on how to process from this point.

I know it's not alot, but I need help to get going.

$users = import-csv "C:\Users\Bruger\Dokumenter\Esport_liste.csv"
Write-Host $users

My CSV look like this: enter image description here

Upvotes: 1

Views: 842

Answers (2)

boxdog
boxdog

Reputation: 8442

Here's one possible solution:

Say, you have a CSV like this:

Player
David
Mary
Thomas
Alice
Michael
Gordon
Gary
Hannah
Sally
Richard
Colin
Emma
Paige
John
Alex

Then you can get some random teams from this as follows:

$players = Import-Csv .\players.csv
$teamSize = 3
$maxTeams = [math]::Floor($players.Count/$teamSize)

$teams = @{}

$shuffled = $players | Get-Random -Count $players.Count

$shuffled |
    ForEach-Object { $i = 0 }{
        $teams["$($i % $maxTeams)"] += @($_.Player)
        $i++
    }

This leads to a hashtable similar to this:

Name Value                  
---- -----                  
0    {Alice, David, Mary}   
1    {Gordon, Colin, John}  
2    {Emma, Paige, Thomas}  
3    {Alex, Hannah, Richard}
4    {Sally, Michael, Gary} 

Adjust the $teamSize if you need more or fewer players in each team.

EDIT: Update based on @mklement0's comments. The above isn't exact in that it won't always give team sizes matching the original requirement. For example, for a list of 22 players:

TeamSize   Teams
--------   -----
1          1 x22
2          2 x11
3          3 x6 / 4 x1
4          4 x3 / 5 x2
5          5 x2 / 6 x2
6          7 x2 / 8 x1
7          7 x2 / 8 x1
8          11 x2
9          11 x2

It does, however, produce more evenly balanced teams if the $teamSize isn't too close to half the total. For a strict team size of 5, for example, you would end up with 4 teams of 5 and 1 team of 2, which might be too much of a mis-match depending on the scenario, but this gives 2 teams of 5 and 2 of 6, which might be 'fairer'.

Anyway, @mklement0 's enhancements produce a more strict adherance to the requirement. Here's the code for that:

$players = Import-Csv .\players.csv
$teamSize = 3
$maxTeams = [math]::Ceiling($players.Count/$teamSize)

$teams = @{}

$shuffled = $players | Get-Random -Count $players.Count

$shuffled |
    ForEach-Object { $i = 0 }{
        $teams["$([Math]::Floor($i / $teamSize))"] += @($_.Player)
        $i++
    }

For comparison, here's the teams this produces:

TeamSize   Teams
--------   -----
1          1 x22
2          2 x11
3          3 x7 / 1 x1
4          5 x4 / 2 x1
5          5 x4 / 2 x1
6          6 x3 / 4 x1
7          7 x3 / 1 x1
8          8 x2 / 6 x1
9          9 x2 / 4 x1

Upvotes: 3

TobyU
TobyU

Reputation: 3918

A possible solution would be:

$users = import-csv "C:\Users\Bruger\Dokumenter\Esport_liste.csv" -Header "User"

$i = 0
$team = 1
$users | Sort-Object { Get-Random } | foreach {

    if(($i % 3) -eq 0){ " "; "Team $($team)"; $team++ }
    $_.User
    $i++
}

I won't explain it further. Try to understand it and ask in the comments, if further help is needed.

Upvotes: 0

Related Questions