Random Number Generator in C

In C, you can use the rand() function from the stdlib.h library to generate a random number. To get a random number within a specific range, you might need to perform some additional operations. Here's an example to generate a random integer between 1 and 10, inclusive:


                 
                #include <stdio.h>
                #include <stdlib.h>
                #include <time.h>

                int main() {
                    // Initialize random seed
                    srand(time(NULL));

                    // Generate a random number between 1 and 10
                    int randomNumber = rand() % 10 + 1;

                    // Print the random number
                    printf("Random Number: %d
", randomNumber);

                    return 0;
                }
                               
                

In this code:

Preset number shortcuts between

Digit shortcuts

Code samples