c++;随机数生成不是随机的 我试图用Visual Studio 2013 C++来执行向量的随机洗牌。下面是我的代码 static void shuffle(vector<int>& a){ int N = a.size(); unsigned long long seed = chrono::system_clock::now().time_since_epoch().count(); default_random_engine generator(seed); for (int i = 0; i < N; i++){ uniform_int_distribution<int> distribution(0,(N-1)-i); int r = i + distribution(generator); swap(a[i], a[r]); } } static void shuffle(向量&a){ int N=a.size(); 无符号长种子=时钟::系统时钟::现在(); 默认随机引擎生成器(种子); 对于(int i=0;i
我的问题是,当我连续多次调用此方法时,洗牌不是随机的。代码可能有什么问题c++;随机数生成不是随机的 我试图用Visual Studio 2013 C++来执行向量的随机洗牌。下面是我的代码 static void shuffle(vector<int>& a){ int N = a.size(); unsigned long long seed = chrono::system_clock::now().time_since_epoch().count(); default_random_engine generator(seed); for (int i = 0; i < N; i++){ uniform_int_distribution<int> distribution(0,(N-1)-i); int r = i + distribution(generator); swap(a[i], a[r]); } } static void shuffle(向量&a){ int N=a.size(); 无符号长种子=时钟::系统时钟::现在(); 默认随机引擎生成器(种子); 对于(int i=0;i,c++,random,C++,Random,我的问题是,当我连续多次调用此方法时,洗牌不是随机的。代码可能有什么问题 任何帮助都将不胜感激。嗯,我很好奇。。。为什么以下内容不足以满足您的需求: static void shuffle(vector<int>& a) { // There are better options for a seed here, but this is what you used // in your example and it's not horrible, so we'
任何帮助都将不胜感激。嗯,我很好奇。。。为什么以下内容不足以满足您的需求:
static void shuffle(vector<int>& a)
{
// There are better options for a seed here, but this is what you used
// in your example and it's not horrible, so we'll stick with it.
auto seed (std::chrono::system_clock::now().time_since_epoch().count());
// Don't bother writing code to swap the elements. Just ask the standard
// library to shuffle the vector for us.
std::shuffle(std::begin(a), std::end(a), std::default_random_engine(seed));
}
static void shuffle(向量&a)
{
//这里有更好的种子选择,但这是您使用的
//在你的例子中,这并不可怕,所以我们会坚持下去。
自动种子(std::chrono::system_clock::now().time_since_epoch().count());
//不必费心编写代码来交换元素,只需询问标准
//库为我们洗牌向量。
std::shuffle(std::begin(a)、std::end(a)、std::default_random_引擎(seed));
}
std::shuffle
dosent删除重复项,它只是交换生成的随机数的位置
否则,您可以自行编写洗牌代码:
#include <ctime>
#include <string>
#include <vector>
#include <iostream>
using namespace std;
void myShuffleWithNoRepeats( int random_once_buf[] , int size=100)
{
srand(time(0));
for (int i=0;i<size;i++)
{
// call made to rand( ) , stored in random_once_buf[ ]
random_once_buf[i]=rand() % 100;
//////////////////////////////////////////////////////////////////////////////////////
// The line below generates unique random number only once //
// //
// the variable i is the random_once_buffer[i] buffer array index count, //
// j is the check for duplicates, j goes through the random_once_buffer[i] buffer //
// from 0 to i at every iteration scanning for duplicates, reversing one step if one duplicate is found.. //
//////////////////////////////////////////////////////////////////////////////////////
for(int j=0;j<i;j++) if (random_once_buf[j] == random_once_buf[i]) i--;
}
cout<<" \n\n\n ";
}
int main(void)
{
const int size=100 ;
int random_once_buffer[100] ;
// Call made to function myShuffleWithNoRepeats( )
myShuffleWithNoRepeats( random_once_buffer , size );
// Loop to display the array random_once_buffer[ ]
for ( int i=0;i<size;i++) cout<<""<<random_once_buffer[i]<<"\t";
cout<<" \nPress any key to continue\n";
cin.ignore();
cin.get();
return 0;
}
#包括
#包括
#包括
#包括
使用名称空间std;
void myShuffleWithNoRepeats(int random_once_buf[],int size=100)
{
srand(时间(0));
对于(int i=0;通常您希望使用random\u设备
为生成器种子。您所说的“非随机”是什么意思?Bartek Banachewicz-random_设备工作正常。Thank.Alexavsavalexandrov-它重复生成相同的数字。如果连续多次调用它会生成相同的序列,那么您可能在所选时钟的“滴答”周期内多次调用它,这意味着now()
将在每次调用时返回相同的值,并且您的随机生成器每次都将使用相同的值作为种子。当然,这是否发生取决于使用的时钟-我认为您可以使用period()
查询刻度长度?这就足够了,但我想知道为什么它没有执行随机洗牌。random\u设备
是一个更好的种子,因为“任何考虑产生随机数字的算术方法的人,当然都处于罪恶状态”。此代码完成了与原始代码完全不同的操作。原始代码洗牌现有整数列表。您的代码生成新的随机整数列表。