RAND48(3)

πŸ“¦
version 15.1 Β· 2 versions
, in freebsd 15.1

πŸ’… rand48(3) πŸ’…

okay so you opened the rand48(3) page from the Library Functions Manual, and bestie, we are about to get into some vintage pseudo-random realness. no cap, this family is iconic in a very specific old-school way.

πŸŽ€ NAME πŸŽ€

drand48, erand48, lrand48, nrand48, mrand48, jrand48, srand48, seed48, lcong48 β€” pseudo random number generators and initialization routines ✨

πŸ“š LIBRARY πŸ“š

Standard C Library (libc, -lc) 🫢

✨ SYNOPSIS ✨

#include <stdlib.h>

double
drand48(void);

double
erand48(unsigned short xseed[3]);

long
lrand48(void);

long
nrand48(unsigned short xseed[3]);

long
mrand48(void);

long
jrand48(unsigned short xseed[3]);

void
srand48(long seed);

unsigned short *
seed48(unsigned short xseed[3]);

void
lcong48(unsigned short p[7]);

πŸ’– DESCRIPTION πŸ’–

🚨 real talk: the functions described in this manual page are not cryptographically secure. Cryptographic applications should use arc4random(3) instead. periodt. do not use these for secrets, bestie.

The rand48() family of functions generates pseudo-random numbers using a linear congruential algorithm working on integers 48 bits in size. the formula is literally:

r(n+1) = (a * r(n) + c) mod m

and the default values are:

  • multiplicand a = 0x5deece66d = 25214903917
  • addend c = 0xb = 11
  • modulo is always fixed at m = 2 ** 48

r(n) is called the seed of the random number generator. she's the main character of this whole operation πŸ’….

For all the six generator routines described next, the first computational step is to perform a single iteration of the algorithm. yes, every single one. no skipping.

drand48() and erand48() return values of type double. the full 48 bits of r(n+1) are loaded into the mantissa of the returned value, with the exponent set such that the values produced lie in the interval [0.0, 1.0). so cute, so floaty πŸ’–.

lrand48() and nrand48() return values of type long in the range [0, 2**31-1]. the high-order (31) bits of r(n+1) are loaded into the lower bits of the returned value, with the topmost (sign) bit set to zero. very demure, very positive.

mrand48() and jrand48() return values of type long in the range [-2**31, 2**31-1]. the high-order (32) bits of r(n+1) are loaded into the returned value. she can go negative, okay? range is range.

The drand48(), lrand48(), and mrand48() functions use an internal buffer to store r(n). for these functions the initial value is r(0) = 0x1234abcd330e = 20017429951246. iconic starting number, honestly.

On the other hand, erand48(), nrand48(), and jrand48() use a user-supplied buffer to store the seed r(n), which consists of an array of 3 shorts, where the zeroth member holds the least significant bits. you bring the seed, they bring the math 🧚.

All functions share the same multiplicand and addend. one algorithm, one vibe.

srand48() is used to initialize the internal buffer r(n) of drand48(), lrand48(), and mrand48() such that the 32 bits of the seed value are copied into the upper 32 bits of r(n), with the lower 16 bits of r(n) arbitrarily being set to 0x330e. additionally, the constant multiplicand and addend of the algorithm are reset to the default values given above. so she seeds and she resets. multitasking queen πŸ‘‘.

seed48() also initializes the internal buffer r(n) of drand48(), lrand48(), and mrand48(), but here all 48 bits of the seed can be specified in an array of 3 shorts, where the zeroth member specifies the lowest bits. again, the constant multiplicand and addend of the algorithm are reset to the default values given above. seed48() returns a pointer to an array of 3 shorts which contains the old seed. this array is statically allocated, thus its contents are lost after each new call to seed48(). so do not expect that little array to stick around, bestie 😭.

Finally, lcong48() allows full control over the multiplicand and addend used in drand48(), erand48(), lrand48(), nrand48(), mrand48(), and jrand48(), and the seed used in drand48(), lrand48(), and mrand48(). an array of 7 shorts is passed as argument:

  • the first three shorts are used to initialize the seed
  • the second three are used to initialize the multiplicand
  • the last short is used to initialize the addend

It is thus not possible to use values greater than 0xffff as the addend. that's the limit, no cap. 0xffff is the ceiling and she is not moving πŸ’€.

🌸 note that all three methods of seeding the random number generator always also set the multiplicand and addend for any of the six generator calls. every time. it's giving package deal.

πŸ‘€ SEE ALSO πŸ‘€

🧚 STANDARDS 🧚

The functions described in this page are expected to conform to IEEE Std 1003.1-2008 (β€œPOSIX.1”). slay, standardization πŸ’….

πŸ‘‘ AUTHORS πŸ‘‘

Martin Birgmeier


Debian Β· September 11, 2025 Β· RAND48(3) β€” that’s the source/version and date, bestie; now go seed responsibly πŸ’–

πŸ“¦ where this page comes from
  • freebsd 15.1: base 15.1
  • πŸ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms