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() ); 从以上链接注意:
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- ruby – 数据库连接池
- 多项目相互依赖的Java程序打包并引入外部Jar包方式
- 如何在Vala中使用CCode属性?
- error 25541 failed to open xml file c:\windows&#
- objective-c – 在objc ARC下用-Os过度释放,而不是-O0
- Swift——Command failed due to signal: Segmentation fau
- ajax传递参数中文乱码问题
- PostgreSQL数据库的密码丢失
- 解决Binary XML file line #6: : Error inflating class <
- c# – 为什么返回的DataTable在FileHelpers中只有readonly列