-3

Okay, I'm writing a C program for a tennis tournament and I need a way to randomly select one of the elements from the array to play against another.

so basically i need something like: char contestant(rand) v char contestant (rand)

instead of just contestant(1) v contestant(2)

  • You should look up shuffling algorithms. There are several posts about them here on SO. – AntonH Nov 17 '14 at 19:12
  • 1
    possible duplicate of [How to use rand function to](http://stackoverflow.com/questions/4270382/how-to-use-rand-function-to) – 2501 Nov 17 '14 at 19:12
  • Just google "c random element from array". This is a pretty common operation. – AtlasMeh-ed Nov 17 '14 at 19:15

1 Answers1

1

If you mean you need 2 random indices from set of N but not same 2:

int c1 = rand()%N;
int c2 = (c1+1+rand()%(N-1))%N;
Anonymous
  • 2,122
  • 19
  • 26