RANDOM(9)
β¨ RANDOM(9) β¨ β the kernel dev manual page, but make it cute
okay bestie, so this is RANDOM(9) from the Kernel Developer's Manual π and it's literally the whole tea on how the kernel hands out pseudo-random numbers. sit down, hydrate, let's go π«Ά
π NAME π
arc4rand, arc4random, random, read_random, read_random_uio, srandom β supply pseudo-random numbers β¨
πΈ SYNOPSIS πΈ
#include <sys/libkern.h>
void
srandom(u_long seed);
u_long
random(void);
void
arc4rand(void *ptr, u_int length, int reseed);
uint32_t
arc4random(void);
#include <sys/random.h>
int
read_random(void *buffer, int count);
int
read_random_uio(struct uio *uio, bool nonblock);
π DESCRIPTION π
so random() is the basic girlie. by default it spits out a sequence of numbers that you can fully duplicate by calling srandom() with some constant as the seed. like, same seed, same vibes, every time.
srandom() can be called with any arbitrary seed value if you want slightly more unpredictable numbers. but listen β it is important to remember that random() is entirely predictable, so it is not of use where knowledge of the sequence of numbers may be of benefit to an attacker. it's giving π© predictability π© and that's on purpose, bestie.
then we have arc4rand(), the main character π. it returns very good quality random numbers, way better suited for security-related purposes. the numbers from arc4rand() are seeded from the entropy device if it is available. automatic reseeds happen after a certain timeinterval β° and after a certain number of bytes have been delivered π¦. and if you want to force it? a forced reseed can be forced by passing a non-zero value in the reseed argument. yes, "forced can be forced", we said what we said.
read_random() is the niche one. it returns entropy directly from the entropy device if it has been loaded. if the entropy device is not loaded, then the buffer is ignored and zero is returned π. the buffer is filled with no more than count bytes.
β οΈ it is strongly advised thatread_random()is not used. instead usearc4rand()β unless it is necessary to know that no entropy has been returned. iykyk.
read_random_uio() behaves identically to read(2) on /dev/random. the uio argument points to a buffer where random data should be stored. this function only returns data if the random device is seeded. it blocks if unseeded π΄, except when the nonblock argument is true.
and here's a lil fun fact that ate π
β all the bits returned by random(), arc4rand(), read_random(), and read_random_uio() are usable. for example, βrandom()&01β will produce a random binary value. slay.
finally, arc4random() is just a convenience function which calls arc4rand() to return a 32 bit pseudo-random integer. it's the little sister function, doing the most with the least β¨
π₯ RETURN VALUES π₯
random() uses a non-linear additive feedback random number generator employing a default table of size 31 containing long integers to return 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). huge. astronomical. iconic.
arc4rand() uses the RC4 algorithm to generate successive pseudo-random bytes. arc4random() uses arc4rand() to generate pseudo-random numbers in the range from 0 to (2**32)β1 π
read_random() returns the number of bytes placed in buffer.
read_random_uio() returns zero when successful β
, otherwise an error code is returned π
π ERRORS π
read_random_uio() may fail if:
[EFAULT]β uio points to an invalid memory region. not it π[EWOULDBLOCK]β the random device is unseeded and nonblock istrue. bestie it literally said "not right now" π«
π AUTHORS π
- Dan Moschuk wrote
arc4random(). - Mark R V Murray wrote
read_random().
absolute legends, both of them π
π and that's the whole page, bestie. no cap, every flag, every default, every range, every error β all accounted for. now go forth and arc4rand() responsibly. periodt. π
this came from the Debian edition of the manual, dated July 16, 2015 π mwah, bye π¦πΈ
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 2015 Mark R V Murray
- Copyright (c) 2000 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms