Skip to content
Tutorials Center
My learning

Topics

C Reference ยท stdlib.h

C rand() and srand() functions

Reference for C rand(), srand() and RAND_MAX: how the generator works, seeding, picking numbers in a range without bias, and why rand is not for security.

rand() returns the next number of a pseudo-random sequence, an int from 0 to RAND_MAX. srand() sets the seed, which decides where the sequence starts. The same seed always produces the same sequence on the same implementation, which is handy for reproducible tests and useless as a source of unpredictability.

If you never call srand(), the program behaves as if it had called srand(1).

C
#include <stdlib.h>

int rand(void);
void srand(unsigned int seed);

Try it

C Same seed, same sequence

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

C A fair number in a range

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with scanf or fgets), it gets the next line from this box.

Syntax

C
int  rand(void);
void srand(unsigned int seed);
#define RAND_MAX /* implementation-defined, at least 32767 */
Name Description
seed Starting value for the sequence. Equal seeds give equal sequences.

Returns: rand() returns an integer in [0, RAND_MAX]. srand() returns nothing.

Errors / undefined behavior: none. Whether rand() is thread-safe is implementation-specific (the standard allows a data race, glibc uses a lock).

Usage notes

  • Seed once, near the start of main. Typical seeds are time(NULL) or a value read from the operating system. Calling srand(time(NULL)) repeatedly inside a loop restarts the sequence with the same value and yields repeated numbers.
  • The sequence is platform specific. For seed 7 this runner's C library (glibc) printed 4 4 6 2 6 4 for six dice rolls; another library gives other numbers for the same seed. Never hard-code expected random values in code that must run elsewhere.
  • RAND_MAX is 2147483647 with glibc but only 32767 with Microsoft's C library. Code that assumes the large value breaks on Windows.
  • rand() % n is biased when RAND_MAX + 1 is not a multiple of n: small results occur slightly more often. The rejection loop above removes the bias.
  • Low bits can be weak in old generators. Prefer scaling (rand() / (RAND_MAX + 1.0) * n) over the remainder when n is large relative to RAND_MAX.
  • Not for security. The sequence is predictable from a few outputs. Keys, tokens and passwords need an operating-system source such as getrandom() or /dev/urandom (POSIX) or a cryptographic library.
  • Shuffling: use Fisher-Yates with an unbiased random_below, not repeated random swaps.
  • Alternatives: POSIX random()/srandom() has a larger state; for Monte Carlo work write a small generator such as xorshift or PCG so results are identical on every platform.