random_shuffle不是真的随机 [英] random_shuffle not really random

查看:415
本文介绍了random_shuffle不是真的随机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在像这样的向量上使用random_shuffle:

I'm using the random_shuffle on a vector like this:

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

运行时,卡座中的内容会混合在一起,但是当我重新启动程序时,会保留该混合后的顺序.

When run, the content of the deck is mixed up, but this mixed-up order is kept when I restart the program.

我错过了什么吗?我如何才能做到真正随机呢?

Did I miss something? How can I make it truly random?

推荐答案

您首先需要使用优待.

#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() );

上面链接中的注释:

通常来说,伪随机数生成器应该仅是 在调用rand()之前先种子一次,然后启动程序. 不应在每次希望播种时重复播种或重新播种 生成一批新的伪随机数.

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.

这篇关于random_shuffle不是真的随机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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