drand48(3)
π drand48(3) β Library Functions Manual π
okay bestie, listen. this is the drand48 page from the Library Functions Manual, and it's genuinely one of the cutest little random-number suites in libc. grab a strawberry π, we're doing 48-bit integer arithmetic and we're gonna love it.
β¨ NAME β¨
drand48, erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48, lcong48 β generate uniformly distributed pseudo-random numbers π²
no cap, that's the whole vibe: pseudo-random numbers, uniformly distributed, all of them.
π LIBRARY π
Standard C library (libc, -lc)
she lives in libc. iconic behavior. π
π SYNOPSIS π
#include <stdlib.h>
double drand48(void);
double erand48(unsigned short xsubi[3]);
long lrand48(void);
long nrand48(unsigned short xsubi[3]);
long mrand48(void);
long jrand48(unsigned short xsubi[3]);
void srand48(long seedval);
unsigned short *seed48(unsigned short seed16v[3]);
void lcong48(unsigned short param[7]);
π¦ Feature Test Macro Requirements for glibc π¦
(if you're confused about these, go read feature_test_macros(7), she explains everything)
All functions shown above:
_XOPEN_SOURCE
|| /* glibc >= 2.19: */ _DEFAULT_SOURCE
|| /* glibc <= 2.19: */ _SVID_SOURCE
yes, that's the exact conditional, don't @ me. β
πΈ DESCRIPTION πΈ
So the way these girls work: they generate pseudo-random numbers using the linear congruential algorithm with 48-bit integer arithmetic. that's the whole personality.
drand48()anderand48()return nonnegativedouble-precision floating-point values, uniformly distributed over the interval[0.0, 1.0). so like, 0 is invited, 1 is NOT. read the bracket bestie, brackets matter πlrand48()andnrand48()return nonnegativelongintegers, uniformly distributed over the interval[0, 2^31).mrand48()andjrand48()return signedlongintegers, uniformly distributed over the interval[-2^31, 2^31). she can go negative, she contains multitudes π
π§ the init girlies
srand48(),seed48(), andlcong48()are initialization functions. you should call one of them before usingdrand48(),lrand48(), ormrand48(). that's the rule. that's the law. πerand48(),nrand48(), andjrand48()do not require an initialization function to be called first. they're self-sufficient. main character energy fr fr.
π² the math part (don't scroll, it's good for you)
All the functions work by generating a sequence of 48-bit integers, Xi, according to the linear congruential formula:
Xn+1 = (aXn + c) mod m, where n >= 0
The parameter m = 2^48, hence 48-bit integer arithmetic is performed.
Unless lcong48() is called, a and c are given by:
a = 0x5DEECE66D
c = 0xB
look at those hex values. look at them. 0x5DEECE66D is lowkey kind of a scrumptious constant. π
The value returned by any of the functions drand48(), erand48(), lrand48(), nrand48(), mrand48(), or jrand48() is computed by first generating the next 48-bit Xi in the sequence. Then the appropriate number of bits, according to the type of data item to be returned, is copied from the high-order bits of Xi and transformed into the returned value. stealing from the top bits only, diva behavior β¨
π internal buffer vs. you-provide-the-storage
drand48(),lrand48(), andmrand48()store the last 48-bit Xi generated in an internal buffer. it's theirs. they keep it. πerand48(),nrand48(), andjrand48()require the calling program to provide storage for the successive Xi values in the array argumentxsubi. so YOU bring the container, bestie, it's a potluck π«Ά- These functions are initialized by placing the initial value of Xi into the array before calling the function for the first time.
π the three initializers, one by one
- The initializer function
srand48()sets the high order 32-bits of Xi to the argumentseedval. The low order 16-bits are set to the arbitrary value0x330E. arbitrary?? more like iconic arbitrary value, she's always0x330Eand she's unbothered π - The initializer function
seed48()sets the value of Xi to the 48-bit value specified in the array argumentseed16v. The previous value of Xi is copied into an internal buffer, and a pointer to this buffer is returned byseed48(). she's handing you her leftovers, and honestly? useful. - The initialization function
lcong48()lets you specify initial values for Xi,a, andc. Array argument elements:param[0-2]specify Xiparam[3-5]specifyaparam[6]specifiesc
After lcong48() has been called, a subsequent call to either srand48() or seed48() will restore the standard values of a and c. so custom params are a temporary era, not a permanent one, noted. π
π₯ ATTRIBUTES π₯
For an explanation of the terms used in this section, see attributes(7). (yes, go read it, it's giving glossary.)
- Interface:
drand48(),erand48(),lrand48(),nrand48(),mrand48(),jrand48(),srand48(),seed48(),lcong48() - Attribute: Thread safety
- Value: MT-Unsafe
race:drand48
β οΈ bestie. the above functions record global state information for the random number generator, so they are not thread-safe. global state = shared state = chaos if you're multithreaded. i'm not being dramatic, the manpage says MT-Unsafe and i'm simply agreeing with her. π
π STANDARDS π
POSIX.1-2008.
certified, standardized, valid, cunty β¨
π¦ HISTORY π¦
POSIX.1-2001, SVr4.
she's been doing this since before you were born. respect your elders. π«Ά
β SEE ALSO β
go read those too, they're the cousins. same family reunion, different table.
and that's the whole page, babe β Linux man-pages 6.20, dated 2026-02-08, filed under drand48(3). ty for reading my little write-up, now go seed your RNG properly. mwah ππΈ
π¦ 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