Random number without duplicates
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | #include <stdio.h> #include <time.h> #define MAXIMUM 100 int main (int argc, char* argv[]) { int Random_Number[MAXIMUM] = { 0, }; int Check_Duplicate[MAXIMUM + 1] = { 0, }; int Number_Of_Times = 18; int maximum = 45; int minimum = 1; if (Number_Of_Times - 1 > maximum - minimum) return -1; (void) srand ((unsigned int) time ((char) 0)); int index = 0; while (index < Number_Of_Times) { int generator = (rand() % maximum) + 1; if (Check_Duplicate[generator] < 1 && generator > minimum - 1) { Check_Duplicate[generator]++; index++; Random_Number[index] = generator; } } for (index = 1; index < Number_Of_Times + 1; index++) { printf ("%3d", Random_Number[index]); if (!(index % 6)) printf ("\n"); } return 0; } | cs |
0 개의 댓글:
댓글 쓰기