For starters I wouldn't recommend using global variables or the keyword using.
Also is there a reason you choose 4 with your random function? what happens when you enter only 3 students? how can it select index 4? also it will never select 5-9.
Also instead of +=1 you can use ++.
One more thing to mention what happens when you have an odd amount of students?
#include <iostream>
#include <vector>
#include <algorithm>
#include <ctime>
int gen( void )
{
staticint x = 0;
return( x++ );
}
int main()
{
std::srand( unsigned( time( NULL ) ) );
constint people = 10;
std::vector<int> vec( people ); //10 people
std::generate( vec.begin() , vec.end() , gen );
for( int i = 0; i < people/2; ++i )
{
std::random_shuffle( vec.begin() , vec.end() );
std::cout << "Person " << vec.back() << std::flush;
vec.pop_back();
std::cout << " is paired with Person " << vec.back() << std::endl;
vec.pop_back();
}
if( people % 2 != 0 ) std::cout << "Person " << vec.front() << " is alone." << std::endl;
return( 0 );
Person 1 is paired with Person 2
Person 5 is paired with Person 9
Person 6 is paired with Person 8
Person 3 is paired with Person 7
Person 4 is paired with Person 0
Process returned 0 (0x0) execution time : 0.270 s
Press any key to continue.