Hi guys im trying to write a program this is the basic spec

2 teams of 11 to be picked at random
and 4 reserves
there are 40 people to chose from.

I am seriously stumped here as I have to pick the two teams, I managed to write a rancom number gen to pick the one team but how do I write the second random number generator to skip the numbers that have already been picked? any ideas?

please give some example code :()

Let's say you have a list of 40 people. You can pick a random one and then mark it as chosen (somehow):

for ( int i = 0; i < n; i++ ) {
  int r;

  do
    r = rand() % 40;
  while ( list[r].chosen );

  list[r].chosen = true;
}

Or save it in a "chosen" list that you search before picking another one:

while ( !done ) {
  int r = rand() % 40;
  int i;

  for ( i = 0; i < n_so_far; i++ ) {
    if ( saved[i] == list[r] )
      break;
  }

  if ( i == n_so_far ) {
    saved[n_so_far++] = list[r];
    done = true;
  }
}

That's not a great solution if you already have the list and need N random items from it. In that case, you can randomly shuffle the list and pick the first N items as your "chosen ones":

#include <algorithm>
std::random_shuffle ( list, list + 40 );

I'll give you a chance at working out how to write your own random shuffle if you can't use the standard library's. It's an amusing and instructive exercise.

Ok I have spend the last couple of hours and I figured out this much

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define N 40


int main()
{

    int i, j;

    /* Set evil seed (initial seed) */
    srand( (unsigned)time( NULL ) );

    for (i = 0; i < 11; i++)
    {
        j = (int) N * rand() / (RAND_MAX + 1.0);
        j=j+1;

        
        printf("%d\n", j);
    }
    
    
    getchar();
    return 0;
}

Now I thought of somthing and me and my freind are basicly banging our heads against the wall over this, the random number gen sometimes repeats the numbers, but I cant have the same number twice, this has just got so much more complecated!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.