What is the distribution for random () in stdlib?

The manual page states that the random function in the C standard library returns a value from 0 to RAND_MAX.

Unfortunately, he does not say what the distribution is for this random function. Empirically, we can measure it as a single, but without documentation, I cannot be sure that it will never change when I use it in applications.

Is distribution documented anywhere?

+4
source share
2 answers

Neither the C standards nor the POSIX specifications define a distribution for random()or rand(). The manipulations for several systems that I tested also do not determine this.

, , , , random() rand() , () , , .

, , random , , ( ), , ( ). , , , - , (, , - PRNG ).

(POSIX)

, POSIX, :

NAME

drand48, erand48, jrand48, lcong48, lrand48, mrand48, nrand48, seed48, srand48 -

#include <stdlib.h>

double drand48(void);
double erand48(unsigned short xsubi[3]);
long jrand48(unsigned short xsubi[3]);
void lcong48(unsigned short param[7]);
long lrand48(void);
long mrand48(void);
long nrand48(unsigned short xsubi[3]);
unsigned short *seed48(unsigned short seed16v[3]);
void srand48(long seedval);

FreeBSD manpage drand48 , arc4random() . arc4random() BSD, - .

+8

random(void) C. (@nwellnhof!) POSIX.

glibc documentation POSIX. .

Glibc:

long int random(void)

: | MT-Safe | AS-Unsafe lock | AC-Unsafe lock | SeeSection 1.2.2.1 [ POSIX], . 2.

. 0 2147483647.

NB: int32_t , 32 , long int . -. 32- .

POSIX:

random() 31 0 2 ^ 31-1. 16 (2 ^ 31-1). . .

256 2 ^ 69.

rand(), random() , srandom() 1 .

srandom() , .

initstate() setstate() . initstate() , , . , , initstate(), , ; , . - 8, 32, 64, 128 256 . , 8 , . , 8 32 (), . seed . initstate() .

initstate() , random() , initstate() seed = 1 size = 128.

initstate() 8 <= size < 32, random() .

, setstate() . , , , initstate() setstate(). setstate() .

, GLIBC, , , SVID, lrand48 :

330:

lrand48() nrand48() , [0, 2 ^ 31).

POSIX.

+3

Source: https://habr.com/ru/post/1692741/


All Articles