C++生成随机数?rand()和srand()的使用

#include 
#include 
#include 

int main()
{
    int min = 33, max = 99;
    // 设置随机数种子
    srand(int(time(0)));
    // 生成 [0, max]
    int x = rand() % (max + 1);
    std::cout << x << std::endl;
    // 生成 [1, max + 1) ,即 [1, max]
    int y = 1 + rand() % (max);
    std::cout << y << std::endl;
    // 生成 [min, max] 范围的随机数
    int z = min + rand() % (max - min + 1);
    std::cout << z << std::endl;

    return 0;
}

你可能感兴趣的:(c++,算法,开发语言)