每次运行程序时,rand()返回相同的数字 [英] rand() returns the same number each time the program is run

查看:192
本文介绍了每次运行程序时,rand()返回相同的数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这个涉及随机数生成的相当基本的C ++代码片段中:

In this rather basic C++ code snippet involving random number generation:

include <iostream>
using namespace std;

int main() {
    cout << (rand() % 100);
    return 0;
}

为什么我总是得到41的输出?我试图让它输出0到100之间的某个随机数.也许我不了解rand函数的工作原理?

Why am I always getting an output of 41? I'm trying to get it to output some random number between 0 and 100. Maybe I'm not understanding something about how the rand function works?

推荐答案

您需要种子"生成器.观看这段简短的视频,它将使一切变得清晰起来.

You need to "seed" the generator. Check out this short video, it will clear things up.

https://www.thenewboston.com/videos.php? cat = 16& video = 17503

这篇关于每次运行程序时,rand()返回相同的数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆