Hi, I'm taking a university class in programming and I'm very new to C. C is my first programming language.
My current assignment is to generate a certain amount of numbers in sequence (i.e. 1-40 or 1-15 etc.) and then present them in random order in an array. Duplicates are NOT allowed.
The numbers are later to be used as starting numbers for athletes in a fictional skiing competition (maximum number of skiers are 47).
My code is working so so, but not all numbers are assigned to an athlete and I also get some skiers assigned with starting number 0 (zero is obviously not allowed as starting number).
I bet there is easier way to do this and I'm open to everything, I just want to get past this obstacleCode:#include <stdio.h> #include <time.h> int main(void) { int i, skier, random, num_skiers=10, found, startpos[46], pos, my_start_pos; srand(time(NULL)); // empty the array for (i = 1; i <=num_skiers; i++) { startpos[i] = 0; } // go through the skiers for (skier = 1; skier <=num_skiers; skier++) { //generate a starting number for the skier my_start_pos = rand() % num_skiers + 1; // is this starting position already occupied? if (startpos[my_start_pos] != 0) { // occupied, find me a new starting position do { my_start_pos = rand() % num_skiers + 1; if (startpos[my_start_pos] == 0) { found = 1; } } while (found !=1); } // assign this starting number to the skier startpos[my_start_pos] = skier; } for (pos = 1; pos <=num_skiers; pos++) { printf("Starting position %d is assigned to skier no. %d\n", pos, startpos[pos]); } return 0; }
Any help is greatly appreciated!



LinkBack URL
About LinkBacks





, you'll get proportional distribution of values, but if you take 1000, you may get some values few times (orders?) more often. So, with enough number of "drawing" back random values, he should get adequate distribution in this situation.
It can be after 10 draws, or after 10000, but there must be some built-in "limit" when full "cycle" is reached, and i don't know it.