TechTorch

Location:HOME > Technology > content

Technology

Generating 10 Unique Random Numbers in a C Program

January 31, 2025Technology4346
How to Generate 10 Unique Random Numbers in a C Program Generating 10

How to Generate 10 Unique Random Numbers in a C Program

Generating 10 unique random numbers in a C program can be achieved by utilizing the rand() function along with a method to ensure the numbers are distinct. This article will guide you through the process, explaining each step and providing a comprehensive example.

Overview of the Approach

To avoid duplicates, you can use a loop that continuously generates random numbers until you have collected 10 unique ones. This process involves checking if a newly generated number is already in the array of random numbers, and if it is not, storing it and incrementing the count.

Include Statements

#include stdio.h - For standard input/output functions. #include stdlib.h - Provides the rand() and srand() functions. #include time.h - For seeding the random number generator with the current time. #include stdbool.h - For using the boolean data type.

Definitions and Constants

define MAX_NUM 100  // Define the range of random numbers

Here, we define the maximum number that can be generated by the random function as 100. You can change this value to adjust the range of numbers.

Function to Check for Unique Numbers

bool is_unique(int num, int arr[], int count) {    for (int i  0; i  count; i  ) {        if (arr[i]  num) {            return false;  // Number is not unique        }    }    return true;  // Number is unique}

This function checks if the given number is already present in the array of previously generated numbers. If the number is found, it returns false; otherwise, it returns true.

Main Function

int main() {    int random_numbers[10];    int count  0;  // Counter for the number of unique numbers    // Seed the random number generator with the current time    srand(time(NULL));    while (count  10) {        int num  rand() % MAX_NUM;  // Generate a random number in the range [0, MAX_NUM)        if (is_unique(num, random_numbers, count)) {            random_numbers[count]  num;  // Store the unique number            count  ;        }    }    // Print the 10 unique random numbers    printf("10 unique random numbers:
");    for (int i  0; i  10; i  ) {        printf("%d ", random_numbers[i]);    }    printf("
");    return 0;}

In the main function:

The random number generator is seeded using srand(time(NULL)) to ensure different sequences of random numbers across runs. A loop runs until 10 unique numbers are generated. Inside the loop, a random number is generated using rand() % MAX_NUM, which ensures the range is within 0 to 99 (if MAX_NUM is 100). The is_unique() function is called to check if the generated number is already in the array of random numbers. If the number is unique, it is added to the array, and the count is incremented. Finally, the 10 generated unique numbers are printed.

Important Notes

Ensure that MAX_NUM is sufficiently large compared to the number of unique numbers you want to generate. If MAX_NUM is too small, the program may enter an infinite loop.

The MAX_NUM defines the upper limit of the range for random numbers. You can adjust this value as needed.

Conclusion

By following this approach, you can generate 10 unique random numbers in a C program. This method can be adapted for any number of unique numbers you need, with only minor adjustments.

If you have any questions or need further assistance, feel free to explore more C programming resources or contact a professional.