0
0
CProgramBeginner · 2 min read

C Program to Generate Random Number with Output Example

Use rand() to generate a random number in C and srand(time(NULL)) to seed the generator for different results each run, like this: srand(time(NULL)); int num = rand();.
📋

Examples

InputNo input needed
OutputRandom number like 1804289383
InputNo input needed
OutputRandom number like 846930886
InputNo input needed
OutputRandom number like 1681692777
🧠

How to Think About It

To generate a random number in C, you first set a starting point called a seed using srand() with the current time so the numbers change each time you run the program. Then you call rand() to get a random number. This number is usually large, so you can use the modulus operator % to limit it to a smaller range if needed.
📐

Algorithm

1
Include necessary headers for input/output and time functions.
2
Seed the random number generator using the current time with <code>srand(time(NULL))</code>.
3
Call <code>rand()</code> to generate a random number.
4
Optionally, use modulus <code>%</code> to limit the random number to a desired range.
5
Print the generated random number.
💻

Code

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

int main() {
    srand(time(NULL));
    int random_number = rand();
    printf("Random number: %d\n", random_number);
    return 0;
}
Output
Random number: 1804289383
🔍

Dry Run

Let's trace the program generating a random number step-by-step.

1

Seed the generator

Call srand(time(NULL)) with current time, e.g., 1680000000, to set the seed.

2

Generate random number

Call rand(), which uses the seed to produce a pseudo-random number, e.g., 1804289383.

3

Print the number

Print the generated number: 1804289383.

StepActionValue
1Seed with time1680000000
2Generate rand()1804289383
3Print outputRandom number: 1804289383
💡

Why This Works

Step 1: Seeding with srand()

Using srand(time(NULL)) sets a different starting point for the random number generator each time the program runs, so numbers vary.

Step 2: Generating with rand()

rand() produces a pseudo-random integer based on the seed, simulating randomness.

Step 3: Printing the result

The program prints the generated number so you can see the random value.

🔄

Alternative Approaches

Generate random number in a range
c
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int main() {
    srand(time(NULL));
    int min = 1, max = 100;
    int random_number = rand() % (max - min + 1) + min;
    printf("Random number between %d and %d: %d\n", min, max, random_number);
    return 0;
}
This limits the random number to a specific range, which is useful for many applications.
Use arc4random() on BSD systems
c
#include <stdio.h>
#include <stdlib.h>

int main() {
    unsigned int random_number = arc4random();
    printf("Random number: %u\n", random_number);
    return 0;
}
arc4random() provides better randomness but is not available on all systems.

Complexity: O(1) time, O(1) space

Time Complexity

Generating a random number with rand() takes constant time because it performs a fixed number of operations.

Space Complexity

The program uses a fixed amount of memory for variables and does not allocate extra space.

Which Approach is Fastest?

Using rand() is fast and simple; alternatives like arc4random() may be slower but offer better randomness.

ApproachTimeSpaceBest For
rand() with srand()O(1)O(1)Simple random numbers, portable
rand() with modulus for rangeO(1)O(1)Random numbers in a specific range
arc4random()O(1)O(1)Better randomness on supported systems
💡
Always seed the random number generator with srand(time(NULL)) to get different numbers each run.
⚠️
Forgetting to call srand() causes rand() to produce the same sequence every time.