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() and erand48() return nonnegative double-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() and nrand48() return nonnegative long integers, uniformly distributed over the interval [0, 2^31).
  • mrand48() and jrand48() return signed long integers, uniformly distributed over the interval [-2^31, 2^31). she can go negative, she contains multitudes πŸ’€

🧚 the init girlies

  • srand48(), seed48(), and lcong48() are initialization functions. you should call one of them before using drand48(), lrand48(), or mrand48(). that's the rule. that's the law. πŸ‘‘
  • erand48(), nrand48(), and jrand48() 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(), and mrand48() store the last 48-bit Xi generated in an internal buffer. it's theirs. they keep it. πŸŽ€
  • erand48(), nrand48(), and jrand48() require the calling program to provide storage for the successive Xi values in the array argument xsubi. 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 argument seedval. The low order 16-bits are set to the arbitrary value 0x330E. arbitrary?? more like iconic arbitrary value, she's always 0x330E and she's unbothered πŸ’…
  • The initializer function seed48() sets the value of Xi to the 48-bit value specified in the array argument seed16v. The previous value of Xi is copied into an internal buffer, and a pointer to this buffer is returned by seed48(). she's handing you her leftovers, and honestly? useful.
  • The initialization function lcong48() lets you specify initial values for Xi, a, and c. Array argument elements:
    • param[0-2] specify Xi
    • param[3-5] specify a
    • param[6] specifies c

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
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms