arc4random(3)
โจ arc4random(3) โจ
okay bestie, buckle up, we're in the Library Functions Manual section (that's the man family this cutie lives in ๐ ) and today's topic is: getting random numbers that aren't mid. like actually, truly, cryptographically random. no cap.
๐ NAME ๐
arc4random, arc4random_uniform, arc4random_buf โ the cryptographically-secure pseudorandom number generator trio. three icons, one vibe check, zero predictability ๐
๐ LIBRARY ๐
Standard C library (libc, -lc). she's built right in, you just gotta show up ๐ซถ
๐ SYNOPSIS ๐
#include <stdlib.h>
uint32_t arc4random(void);
uint32_t arc4random_uniform(uint32_t upper_bound);
void arc4random_buf(size_t n;
void buf[n], size_t n);
๐ธ DESCRIPTION ๐ธ
these functions hand you cryptographically-secure pseudorandom numbers. like, the good stuff. the un-guessable stuff. it's giving security, it's giving chaos, it's giving "how did you even predict that, you can't" ๐
arc4random()โ returns a uniformly-distributed value. every number has the same chance, no favorites, no nepo babies โจarc4random_uniform()โ returns a uniformly-distributed value less thanupper_bound(but pls read the BUGS section below, it's important, i'm not joking ๐)arc4random_buf()โ fills the memory thatbufpoints to withnbytes of pseudorandom data. bulk random, grab a bucket ๐ง
and now the part that's gonna ruffle some feathers: the rand(3) and drand48(3) families of functions? they should only be used where the quality of the pseudorandom numbers is not a concern and there's a need for repeatability of the results. that's a two-condition situation. BOTH of them. if you don't meet both, use the arc4random() functions. no debate, no exceptions, periodt ๐
๐ RETURN VALUE ๐
arc4random()returns a pseudorandom number. simple. clean. iconic.arc4random_uniform()returns a pseudorandom number less thanupper_boundfor valid input, or0whenupper_boundis invalid. note that0on the fail path. remember it. we'll come back to it ๐ญ
๐ฅ ATTRIBUTES ๐ฅ
for an explanation of the terms used in this section, go see attributes(7). but here's the tea:
arc4random(),arc4random_uniform(),arc4random_buf()โ Thread safety: MT-Safe. yes ma'am, safe to call from multiple threads, no drama, no beef ๐
โญ STANDARDS โญ
BSD. that's it, that's the whole section. born on BSD, raised on BSD ๐ฉท
๐ฆ HISTORY ๐ฆ
- OpenBSD 2.1
- FreeBSD 3.0
- NetBSD 1.6
- DragonFly 1.0
- libbsd
- glibc 2.36
look at that glow-up timeline, she's been bookin' since the 90s and she just landed in glibc 2.36 like it was nothing ๐
๐ญ BUGS ๐ญ
okay this is the serious bit, bestie, so listen up fr fr:
an upper_bound of 0 doesn't make sense in a call to arc4random_uniform(). such a call will fail, and return 0.
be careful, because that value is not less than upper_bound. in some cases โ like accessing an array โ using that value could result in undefined behavior. ๐จ
so if you're doing something like using that return value as an array index, you are lowkey stepping into the void. i'm not being dramatic, that's literally what undefined behavior means ๐ please validate your bounds, i'm begging ๐
๐ SEE ALSO ๐
getrandom(3)rand(3)drand48(3)random(7)
go read those too, i know you want to, you're a scholar ๐
and that's the whole page, babe. this lil slice of the Linux man-pages 6.20, dated 2025-06-28, from the arc4random(3) page, is officially yours now โจ go forth and be random (but like, the cryptographically secure kind, 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
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms