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 than upper_bound (but pls read the BUGS section below, it's important, i'm not joking ๐Ÿ™ˆ)
  • arc4random_buf() โ€” fills the memory that buf points to with n bytes 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 than upper_bound for valid input, or 0 when upper_bound is invalid. note that 0 on 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 ๐Ÿ“

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