c – random_shuffle不是真的随机

c – random_shuffle不是真的随机,第1张

概述我在这样一个向量上使用random_shuffle: #include <algorithm>vector <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 hererandom_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 hererandom_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.

总结

以上是内存溢出为你收集整理的c – random_shuffle不是真的随机全部内容,希望文章能够帮你解决c – random_shuffle不是真的随机所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1253473.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存