← Back to C Course | Chapter 14: Additional Topics | Lesson 8 of 9

C Random Numbers

Random numbers are unpredictable-seeming values a program makes, like rolling dice. You give rand a starting seed so the sequence is different each time.
Syntax
c
#include <stdlib.h>
#include <time.h>

srand(time(NULL));         // seed once at program start
int n = rand();            // 0 to RAND_MAX
int in_range = rand() % max + min;

Generating Numbers with rand()

rand(), declared in stdlib.h, returns a pseudo-random integer somewhere between 0 and the implementation-defined constant RAND_MAX each time it's called, generating numbers from an internal algorithm rather than any genuinely unpredictable physical source.

Example: Generating Numbers with rand()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	// Declare n and set it to rand()
	int n = rand();
	// Print formatted text to the screen
	printf("%d", n >= 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Setting the Seed with srand()

Without calling srand() first, rand() always starts from the exact same internal state and therefore produces the identical sequence of numbers on every single run of the program, which is useful for reproducible testing but not for anything meant to feel random to a user.

Example: Setting the Seed with srand()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	srand(1);
	// Declare n and set it to rand()
	int n = rand();
	// Print formatted text to the screen
	printf("%d", n >= 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Randomizing with time()

Seeding srand() with the current system time, via srand(time(NULL));, is the standard way to make each run of a program produce a different sequence of random numbers, since the current time is different (and effectively unpredictable) every time the program starts.

Example: Randomizing with time()

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
#include <time.h>
// Program execution starts in main()
int main() {
	srand(time(NULL));
	// Declare n and set it to rand()
	int n = rand();
	// Print formatted text to the screen
	printf("%d", n >= 0);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Limiting Random Ranges

Applying the modulo operator to rand()'s output, such as rand() % 10, restricts the generated values to a smaller range (0 through 9 in this example), which is the usual technique for generating random numbers within specific bounds like dice rolls or array indices.

Example: Limiting Random Ranges

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	srand(1);
	// Declare n and set it to rand() % 10
	int n = rand() % 10;
	// Print formatted text to the screen
	printf("%d", n >= 0 && n < 10);
	// Return 0 to signal that the program finished successfully
	return 0;
}

Generating Random Floats

Dividing rand()'s integer result by the floating-point constant RAND_MAX produces a random decimal value between 0.0 and 1.0, which is the standard building block for generating random floats or percentages, or for scaling into any other custom floating-point range you need.

Example: Generating Random Floats

c
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Include general utilities (malloc, free, exit)
#include <stdlib.h>
// Program execution starts in main()
int main() {
	srand(1);
	// Declare f and set it to rand() / (double)RAND_MAX
	double f = rand() / (double)RAND_MAX;
	// Print formatted text to the screen
	printf("%d", f >= 0.0 && f <= 1.0);
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Calling rand() without seeding with srand(), so each run gives the same numbers.
  2. Calling srand(time(NULL)) inside a loop, which resets the seed with the same value each time.
  3. Using rand() % n and expecting a perfectly even distribution.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.