是否可以预测srand的随机数(时间(0))? [英] Is it possible to predict random number of srand(time(0))?

查看:300
本文介绍了是否可以预测srand的随机数(时间(0))?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的兰登数是rand()%6 + 1 aka骰子滚动,当它基于时间,是否有可能使一个控制台应用程序预见未来的数字在我想要的时间?例如在未来的某一天预测一个时间14:40:32的数字?

my rand number is rand()%6+1 aka dice rolling, when its based on "time", is it possible to make a console app that foresees the future numbers in the time I want to? for example predict a number on time 14:40:32 on a certain day in future?

推荐答案

相同的实现 rand ie链接到同一版本的标准库。所有你需要的是获得 time_t 值,你感兴趣的时间传递给 srand 并调用<$

Yes provided that you use the same implementation of rand i.e. link with the same version of the standard library. All you need is to get the time_t value for the time you are interested in pass it to srand and call rand to get the value.

例如,如果 time_t 保存从纪元开始的秒数(对于大多数实现来说是这种情况),那么您可以执行以下操作以获取 rand 返回的值,未来种子:

For example, if time_t holds the number of seconds since the epoch (which is the case for most implementations), then you can do the following to get the value returned by rand with a 10-second-in-the-future seed:

std::srand(std::time(nullptr) + 10);
std::cout << std::rand();

(不提出是否使用 rand

这篇关于是否可以预测srand的随机数(时间(0))?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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