Does stdlib rand () always give the same sequence?

I really like being able to generate the same set of pseudo-random data repeatedly, especially with experimental code setup. Through observation, I would say that rand()it seems to give the same sequence of numbers every time *.

Is it guaranteed for repeated executions on the same computer / for different machines / for different architectures?

* For the same seed is obvious.

+5
source share
6 answers

Yes, given the same environment for the program. From standard C, 7.20.2.2/2,

srand , rand. srand , . rand , - srand, , srand 1.

, , (.. , ). C , , C, .

. , .

+18

, srand() - . , , , .

" " , , .

, PRNG .

+2

.

C :

srand , .

, - .

, rand() ​​ . , - .

+1

, srand / . " ", . , , , srand .

0

/ /, . drand48. .

0

UNIX/Linux, drand48() srand48 () , . - C. /usr/include/stdlib.h. , .

srand48() i.e. srand48 (2), dran48() for, . .

include stdio.h
include stdlib.h
double drand48();
int main(void){
    int i;
    double rn;
    srand48(2);
    for(i=0; i<10; i++){
        randNum = drand48();
        printf("%.6l\n", randNum);
        return 0;
}
0

All Articles