RANDOM(3)

๐Ÿ“ฆ
version 15.1 ยท 2 versions
, in freebsd 15.1

okay so this is RANDOM(3), from the Library Functions Manual, and it's the whole group-chat recap for random, srandom, srandomdev, initstate, and setstate. Bestie, it's giving non-cryptographic pseudorandom number generator; routines for changing generators. ๐Ÿ’…

๐ŸŽ€ NAME ๐ŸŽ€

random, srandom, srandomdev, initstate, setstate โ€” non-cryptographic pseudorandom number generator; routines for changing generators. That's the whole cast, no cap. ๐Ÿ’–

๐Ÿฆ‹ LIBRARY ๐Ÿฆ‹

Standard C Library (libc, -lc). Slay. โœจ

๐ŸŒธ SYNOPSIS ๐ŸŒธ

#include <stdlib.h>

long
random(void);

void
srandom(unsigned int seed);

void
srandomdev(void);

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

char *
setstate(char *state);

๐Ÿ’– DESCRIPTION ๐Ÿ’–

bestie, these functions are not secure. If your app needs unpredictable random numbers, use arc4random(3) instead. Periodt. ๐Ÿ”ฅ

Unless it's initialized with less than 32 bytes of state, random() uses a non-linear additive feedback random number generator with a default table of size 31 long integers, returning successive pseudo-random numbers in the range from 0 to (2**31)โˆ’1. The period of this random number generator is very large, approximately 16*((2**31)โˆ’1). Iconic longevity behavior. ๐Ÿ‘‘

If initialized with less than 32 bytes of state, random() uses the poor-quality 32-bit Park-Miller LCG. Not the vibe for serious unpredictability, iykyk. ๐Ÿ’€

The random() and srandom() functions are analogous to rand(3) and srand(3). Like rand(3), random() is implicitly initialized as if srandom(1) had been invoked explicitly. Cute little default, no notes. ๐ŸŽ€

The srandomdev() routine initializes the state array using random numbers obtained from the kernel. This can generate states which are impossible to reproduce by calling srandom(), because the succeeding terms in the state buffer are no longer derived from the Park-Miller LCG algorithm applied to a fixed seed. Kernel chaos is lowkey the main character here. ๐ŸŒˆ

The initstate() routine initializes the provided state array of uint32_t values and uses it in future random() invocations. (Despite the char * type of state, the underlying object must be a naturally aligned array of 32-bit values.) The size of the state array (in bytes) is used by initstate() to decide how sophisticated a random number generator it should use โ€” the more state, the better the random numbers will be. (Current "optimal" values for the amount of state information are 8, 32, 64, 128, and 256 bytes; other amounts will be rounded down to the nearest known amount. Using less than 8 bytes will cause an error.) The seed is used as in srandom(). The initstate() function returns a pointer to the previous state information array. Bestie, it's literally handing you the old state. ๐Ÿซถ

The setstate() routine switches random() to using the provided state. It returns a pointer to the previous state. Little state swap moment. ๐Ÿฆ‹

Once a state array has been initialized, it may be restarted at a different point either by calling initstate() (with the desired seed, the state array, and its size) or by calling both setstate() (with the state array) and srandom() (with the desired seed). The advantage of calling both setstate() and srandom() is that the size of the state array does not have to be remembered after it is initialized. That's the low-effort glow-up. โœจ

With 256 bytes of state information, the period of the random number generator is greater than 2**69 which should be sufficient for most purposes. That's a lot of random, bestie. ๐Ÿ‘‘๐Ÿ’œ

๐Ÿ’€ DIAGNOSTICS ๐Ÿ’€

If initstate() is called with less than 8 bytes of state information, or if setstate() detects that the state information has been garbled, NULL is returned. So NULL means something went wrong, mkay? ๐Ÿ™ˆ

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ

๐ŸŒŸ HISTORY ๐ŸŒŸ

These functions appeared in 4.2BSD. Vintage, but still doing its thing. ๐ŸŽ€

๐Ÿ‘‘ AUTHORS ๐Ÿ‘‘

Earl T. Cohen. Give them their flowers. ๐ŸŒธ


okay that's the whole page, bestie. The footer says this one's from Debian, dated February 1, 2020 โ€” historical receipt, mwah. ๐Ÿซถ๐Ÿ’…

๐Ÿ“ฆ 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