1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
#include <iostream>
#include <vector>
#include <string>
#include <random>
#include <algorithm>
#include <iomanip>
std::vector< std::vector<std::string> >
split_into_random_groups( std::vector<std::string> population, std::size_t n_groups )
{
if( n_groups == 0 ) return {} ;
if( n_groups == 1 ) return { population } ;
// if there is no genuine random_device (eg. GNU/MinGW) seed with current time
static std::mt19937 rng{ std::random_device{}() } ;
std::shuffle( std::begin(population), std::end(population), rng ) ;
std::vector< std::vector<std::string> > groups(n_groups) ;
for( std::size_t i = 0 ; i < population.size() ; ++i )
groups[i%n_groups].push_back( population[i] ) ;
// simulate assigning the residue evenlt to random groups
std::shuffle( std::begin(groups), std::end(groups), rng ) ;
return groups ;
}
void print_groups( const std::vector< std::vector<std::string> >& groups )
{
std::cout << "#groups: " << groups.size() << '\n' ;
for( const auto& grp : groups )
{
std::cout << " [ " ;
for( const auto& str : grp ) std::cout << std::quoted(str) << ' ' ;
std::cout << "] (" << grp.size() << ")\n" ;
}
std::cout << "---------------------\n" ;
}
int main()
{
std::vector<std::string> people =
{
"Alex Garza", "Alex Ward", "Andrew Matthew", "Anthony Williams", "Brandon Kolak",
"Christopher Herbstreit", "Christopher King", "Damani Holsendolph", "Dylan Correia",
"James Frew", "Jody Munyon", "Joseph Radecki", "Michael Randolph", "Robert Hafner",
"Sixx Morrow", "Spirit Manley", "Zach Philipp"
};
for( std::size_t n_groups : { 3, 4, 5, 6 } )
print_groups( split_into_random_groups( people, n_groups ) ) ;
}
|