相关疑难解决方法(0)

为什么rand()在每次运行时产生相同的数字序列?

每次我用rand()它运行程序都会给我相同的结果.

示例:

#include <iostream>
#include <cstdlib>

using namespace std;

int random (int low, int high) {
    if (low > high) return high;
    return low + (rand() % (high - low + 1));
}
int main (int argc, char* argv []) {
    for (int i = 0; i < 5; i++) cout << random (2, 5) << endl;
}
Run Code Online (Sandbox Code Playgroud)

输出:

3
5
4
2
3
Run Code Online (Sandbox Code Playgroud)

每次运行程序时,每次都输出相同的数字.有没有解决的办法?

c++ random

34
推荐指数
2
解决办法
8万
查看次数

标签 统计

c++ ×1

random ×1