下面的代码正在编译和运行。代码应该将Blitz矩阵初始化为随机值,但它失败了,因为矩阵的每个元素都获得了相同的值。
#include <iostream>
#include <array>
#include <algorithm>
#include <functional>
#include <random>
#include <blitz/array.h>
int main()
{
// random
std::random_device __device;
std::array<int, std::mt19937::state_size> __seeds;
std::normal_distribution<double> __distribution(0.0, 1.0);
std::mt19937 __engine;
std::generate_n(__seeds.data(), __seeds.size(), std::ref(__device));
std::seed_seq __sequence(std::begin(__seeds), std::end(__seeds));
__engine.seed(__sequence);
// matrix
blitz::Array<float,2> a(4,5);
a=__distribution(__engine);
// io
std::cout << a << std::endl;
}输出结果不是我想要的
(0,3) x (0,4)
[ -1.10231 -1.10231 -1.10231 -1.10231 -1.10231
-1.10231 -1.10231 -1.10231 -1.10231 -1.10231
-1.10231 -1.10231 -1.10231 -1.10231 -1.10231
-1.10231 -1.10231 -1.10231 -1.10231 -1.10231 ]将闪电战矩阵初始化为随机值的正确方法是什么?
发布于 2015-02-04 02:54:45
你说让每个单元格都是这个值,试试这个
#include <iostream>
#include <array>
#include <algorithm>
#include <functional>
#include <random>
#include <blitz/array.h>
int main()
{
// random
std::random_device __device;
std::array<int, std::mt19937::state_size> __seeds;
std::normal_distribution<double> __distribution(0.0, 1.0);
std::mt19937 __engine;
std::generate_n(__seeds.data(), __seeds.size(), std::ref(__device));
std::seed_seq __sequence(std::begin(__seeds), std::end(__seeds));
__engine.seed(__sequence);
// matrix
blitz::Array<float,2> a(4,5);
for( int i=0; i<4; ++i ) {
for( int j=0; j<5; ++j ) {
a(i, j) =__distribution(__engine);
}
}
// io
std::cout << a << std::endl;
}https://stackoverflow.com/questions/28284380
复制相似问题