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);
    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 に答える