#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不是真的随机所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)