random(3)

๐ŸŽฒ random(3) โ€” the manual page, but make it make sense ๐Ÿ’…

okay bestie, so this is the random(3) page from your friendly neighborhood Library Functions Manual, and it lives in the Standard C library (libc, -lc). pull up a chair, we're talking pseudo-random numbers and it's lowkey iconic ๐ŸŽ€


โœจ NAME โœจ

random, srandom, initstate, setstate โ€” random number generator ๐Ÿฆ‹

no fr that's the whole name section. four lil functions. one big vibe.


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library (libc, -lc)


๐Ÿซถ SYNOPSIS ๐Ÿซถ

#include <stdlib.h>

long random(void);
void srandom(unsigned int seed);

char *initstate(size_t n;
                unsigned int seed, char state[n], size_t n);
char *setstate(char *state);

๐ŸŒธ Feature Test Macro Requirements for glibc (see feature_test_macros(7)): ๐ŸŒธ

random(), srandom(), initstate(), setstate():

_XOPEN_SOURCE >= 500
    || /* glibc >= 2.19: */ _DEFAULT_SOURCE
    || /* glibc <= 2.19: */ _SVID_SOURCE || _BSD_SOURCE

๐Ÿ’– DESCRIPTION ๐Ÿ’–

okay so first up, random(). she uses a nonlinear additive feedback random number generator and she's serving a default table of size 31 long integers, handing you successive pseudo-random numbers in the range from 0 to 2^31 - 1. and the period? literally enormous. approximately 16 * ((2^31) - 1). she's committed, periodt ๐Ÿ‘‘

then there's srandom(), which takes its argument and sets it as the seed for a whole new sequence of pseudo-random integers that random() is gonna return. the way that these sequences are repeatable if you call srandom() with the same seed value... obsessed. and if you don't provide a seed at all, random() just auto-seeds itself with a value of 1. she's independent like that ๐Ÿ™ˆ

next, initstate() โ€” she lets you initialize a state array state for random() to use. the size of that state array, n, is what initstate() uses to decide how sophisticated a random number generator it should go with. and bestie, the rule is simple: the larger the state array, the better the random numbers will be โœจ the current "optimal" values for the size of the state array n are 8, 32, 64, 128, and 256 bytes. other amounts? they get rounded down to the nearest known amount. and using less than 8 bytes results in an error, so don't do that ๐Ÿ’€

also seed is the seed for the initialization โ€” it specifies a starting point for the random number sequence, and it provides for restarting at the same point. so you can go back, iykyk ๐Ÿงš

and finally setstate() โ€” she changes the state array that random() is using. the state array state is used for random number generation until the next call to initstate() or setstate(). but important: state must first have been initialized using initstate(), or be the result of a previous call of setstate(). no shortcuts, bestie ๐Ÿซฃ


๐ŸŽ RETURN VALUE ๐ŸŽ

  • random() returns a value between 0 and (2^31) - 1. cute.
  • srandom() returns no value. she gives nothing and takes nothing ๐Ÿ’…
  • initstate() returns a pointer to the previous state array. On failure, it returns NULL, and errno is set to indicate the error. flag it ๐Ÿ˜ญ
  • On success, setstate() returns a pointer to the previous state array. On failure, it returns NULL, and errno is set to indicate the error. same energy fr fr.

๐Ÿ’€ ERRORS ๐Ÿ’€

  • EINVAL โ€” the state argument given to setstate() was NULL. girl, you passed nothing. nothing. ๐Ÿซ 
  • EINVAL โ€” a state array of less than 8 bytes was specified to initstate(). tiny state array. no notes. just no.

๐Ÿ”ฅ ATTRIBUTES ๐Ÿ”ฅ

for an explanation of the terms used in this section, see attributes(7).

  • random(), srandom(), initstate(), setstate() โ€” Thread safety: MT-Safe โœ…

all four of them. MT-Safe. every single one. the whole squad is safe, we love to see it ๐ŸŒˆ


๐Ÿ“œ STANDARDS ๐Ÿ“œ

POSIX.1-2008. certified, bestie ๐Ÿ’–


๐Ÿฆ• HISTORY ๐Ÿฆ•

POSIX.1-2001, 4.3BSD. she's been around. she's got lore. she's vintage ๐Ÿ‘‘


๐Ÿง  NOTES ๐Ÿง 

ngl random-number generation is a complex topic, and if you want the real tea, these two are the reads:

  • Numerical Recipes in C: The Art of Scientific Computing (William H. Press, Brian P. Flannery, Saul A. Teukolsky, William T. Vetterling; New York: Cambridge University Press, 2007, 3rd ed.) โ€” provides an excellent discussion of practical random-number generation issues in Chapter 7 (Random Numbers). ๐Ÿ“
  • For a more theoretical discussion which also covers many practical issues in depth, see Chapter 3 (Random Numbers) in Donald E. Knuth's The Art of Computer Programming, volume 2 (Seminumerical Algorithms), 2nd ed.; Reading, Massachusetts: Addison-Wesley Publishing Company, 1981. ๐Ÿงš

like, actual textbooks. for a random number generator. and honestly? we respect it.


โš ๏ธ CAVEATS โš ๏ธ

heads up bestie: the random() function should not be used in multithreaded programs where reproducible behavior is required. use random_r(3) for that purpose. no cap. this one matters ๐Ÿ”ฅ

๐Ÿ› BUGS ๐Ÿ›

According to POSIX, initstate() should return NULL on error. In the glibc implementation, errno is (as specified) set on error, but the function does not return NULL. so like... the man page said one thing and glibc said another. it's giving discrepancy ๐Ÿ’€ and it's a documented one, so now you know ๐ŸŒธ


๐Ÿ‘€ SEE ALSO ๐Ÿ‘€


<div align="center">and that's the whole page bestie!! go forth and generate numbers responsibly ๐ŸŽฒ๐Ÿ’–โœจ</div>

this diva of a man page is brought to you by Linux man-pages 6.20, dated 2026-02-08, and it's the random(3) page โ€” now go seed something (with srandom(), obviously) ๐ŸŽ€๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from
  • arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
  • nixpkgs unstable: man-pages 6.20
  • ๐Ÿ“œ the page's license: Linux-man-pages-copyleft
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms