If you're looking for an easier or simpler way to do what you are already doing...

This:

Code:

my_start_pos = rand() % num_skiers + 1;
if (startpos[my_start_pos] != 0) {
do {
my_start_pos = rand() % num_skiers + 1;
if (startpos[my_start_pos] == 0) {
found = 1;
}
} while (found !=1);
}

Is equivalent to this:

Code:

do my_start_pos = rand() % num_skiers + 1;
while (startpos[my_start_pos] != 0);

There's no need to mess around with the 'found' flag. However, your randomized algorithm has a non-deterministic bound on runtime -- it could potentially run for a very long time if that last slot never gets chosen by the random number generator (although this is very unlikely).

You could do it a more deterministic way by filling the array with the skiers in order, and then doing a fixed number of random swaps. Pick two random positions in the array and swap them. Do this... lets say num_skiers*2 times and the array should be pretty darn randomized. And it runs in linear time thanks to our choice of running it num_skiers*2 times.

With a small number of skiers like you're using, you aren't going to see any performance impact from using the non-deterministic algorithm. However, up the max number of skiers to a million, and you'll find it grinds to a halt, while the deterministic method handles it just fine.

Here's a simple test program if you're interested. Set METHOD to 1 to use your original way; set it to 2 to use the swapping method I described. (You also might want to comment out the printing... a million skiers might take a while to print )

Code:

#include <stdio.h>
#include <time.h>
// Your original method
#define METHOD 1
// The swapping method
//#define METHOD 2
#define MAX_SKIERS 1000000
int starting_lineup[MAX_SKIERS+1];
int main(void)
{
int i, num_skiers = 1000000;
srand(time(NULL));
#if METHOD == 1
int skier_pos;
//For each skier, assign them a random position in the starting lineup
for (i = 1; i <= num_skiers; i++) {
do skier_pos = rand() % num_skiers + 1;
while (starting_lineup[skier_pos] != 0);
starting_lineup[skier_pos] = i;
}
#elif METHOD == 2
int pos1, pos2, temp;
for (i = 0; i <= num_skiers; i++)
starting_lineup[i] = i;
for (i = 0; i < num_skiers*2; i++) {
// Generate two random positions
pos1 = rand() % num_skiers + 1;
pos2 = rand() % num_skiers + 1;
// Swap the skiers at the two positions
temp = starting_lineup[pos1];
starting_lineup[pos1] = starting_lineup[pos2];
starting_lineup[pos2] = temp;
}
#endif
printf("Method %d\nThe starting lineup (first to last):\n", METHOD);
for (i = 1; i <= num_skiers; i++)
printf("%s%d", (i == 1 ? "" : ", "), starting_lineup[i]);
putchar('\n');
return 0;
}