0

下面的代码正在编译和运行。代码应该将 Blitz-matrix 初始化为随机值,但由于矩阵的每个元素都获得相同的值,它会失败。

#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 ]

将 Blitz-Matrix 初始化为随机值的正确方法是什么?

4

1 回答 1

0

你说让每个单元格都有这个值,试试这个

#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;
}
于 2015-02-03T18:54:44.573 回答