天天看点

srand和rand c语言,有人详解一下srand和rand的用法吗?

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼

C99:TC3

7.20.2 Pseudo-random sequence generation functions

7.20.2.1 The rand function

Synopsis

1 #include

int rand(void);

Description

2 The rand function computes a sequence of pseudo-random integers in the range 0 to RAND_MAX.

3 The implementation shall behave as if no library function calls the rand function.

Returns

4 The rand function returns a pseudo-random integer.

Environmental limits

5 The value of the RAND_MAX macro shall be at least 32767.

7.20.2.2 The srand function

Synopsis

1 #include

void srand(unsigned int seed);

Description

2 The srand function uses the argument as a seed for a new sequence of pseudo-random

numbers to be returned by subsequent calls to rand. If srand is then called with the

same seed value, the sequence of pseudo-random numbers shall be repeated. If rand is

called before any calls to srand have been made, the same sequence shall be generated

as when srand is first called with a seed value of 1.

3 The implementation shall behave as if no library function calls the srand function.

Returns

4 The srand function returns no value.