加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – random_shuffle不是真的随机

发布时间:2020-12-16 03:10:29 所属栏目:百科 来源:网络整理
导读:我在这样一个向量上使用random_shuffle: #include algorithmvector Card deck;//some code to add cards to the deck hererandom_shuffle ( deck.begin(),deck.end() ); 运行时,甲板的内容会混合起来,但是当我重新启动程序时,会保留这个混合顺序. 我错过了
我在这样一个向量上使用random_shuffle:
#include <algorithm>
vector <Card> deck;
//some code to add cards to the deck here
random_shuffle ( deck.begin(),deck.end() );

运行时,甲板的内容会混合起来,但是当我重新启动程序时,会保留这个混合顺序.

我错过了什么?如何让它真正随机?

解决方法

您需要使用 srand先播放伪随机数生成器.
#include <algorithm>
#include <cstdlib>

...

std::srand(std::time(0));

vector <Card> deck;
//some code to add cards to the deck here
random_shuffle ( deck.begin(),deck.end() );

从以上链接注意:

Generally speaking,the pseudo-random number generator should only be seeded once,before any calls to rand(),and the start of the program. It should not be repeatedly seeded,or reseeded every time you wish to generate a new batch of pseudo-random numbers.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读