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 returnsNULL, anderrnois set to indicate the error. flag it ๐ญ- On success,
setstate()returns a pointer to the previous state array. On failure, it returnsNULL, anderrnois set to indicate the error. same energy fr fr.
๐ ERRORS ๐
EINVALโ thestateargument given tosetstate()wasNULL. girl, you passed nothing. nothing. ๐ซEINVALโ a state array of less than 8 bytes was specified toinitstate(). 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: therandom()function should not be used in multithreaded programs where reproducible behavior is required. userandom_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
- Copyright 1993, David Metcalfe <david@prism.demon.co.uk>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms