srand()与rand()生成随机数

srand()与rand()生成随机数

经过测试,当srand的值确定时,其对应的rand值也是确定的。

 

#include <iostream>
using namespace std;
int main()
{
    for (int i = 0; i <= 10; i++) {
        srand(i);
        cout << "当seed = " << i << "时,其结果为:" << rand() << endl;
    }

    cout << endl << endl << "------第二次验证---------" << endl << endl;

    for (int i = 0; i <= 10; i++) {
        srand(i);
        cout << "当seed = " << i << "时,其结果为:" << rand() << endl;
    }
}

 

 

 

 

 

posted @ 2020-01-10 18:48  OneTrainee  阅读(227)  评论(0编辑  收藏  举报