C rand() 并不是真正随机的 [英] C rand() is not really random

查看:78
本文介绍了C rand() 并不是真正随机的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个应用程序.我正在使用:

I'm working on an application. I'm using:

int rand_num = rand() % 100;

获取0到99之间的随机整数.现在,假设我得到 41,然后重新启动我的应用程序,它总是 41.一遍又一遍的相同序列,意味着它不是真正随机的.

To get a random integer between 0 to 99. Now, let's say I got 41, then restarting my application, it's always 41. Same sequence over and over, means it's not really random.

有什么解决办法吗?

推荐答案

rand 生成一个伪随机数,是的.但是你可以设置种子.

rand generates a pseudo-random number, yes. But you can set the seed.

srand(time(NULL));
int rand_num = rand() % 100;

只设置一次种子.

或者您可以使用 .

Or you can use one of the methods from <random>.

这篇关于C rand() 并不是真正随机的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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