2016-04-08 17 views
0

私のクラスは、タイプのいくつかのベクトル行列含ま値を変更されていません。ランダム生成されたベクトルは

typedef std::vector<double> MyArray; 
typedef std::vector<MyArray> MyMatrix; 

クラスupdate()メソッドを持っているが、それぞれに新しいランダム行列を生成する必要があり、それを呼び出すと、古い場所std::map<double, MyMatrix, std::greater<double>> rankedMyMatrix_;

Updateメソッドは、およそ次のようになります。

void MyClass::update(...){ 
    ... 
    this->generateMyMatrix(); // Generates new random currentMyMatrix_ 
    ... 
    rankedMyMatrix_.insert({matrixRank_, currentMyMatrix_}); 
    ... 
} 

方法generateMyMatrix()次のようになります。

void MyClass::generateMyMatrix(){ 
    std::random_device rd; 
    std::mt19937 mt(rd()); 
    std::uniform_real_distribution<double> dist(-1, 1); 
    for (int i = 0; i < noMotors_; i++) { 
     MyArray array(MaxArraySamples, 0); 
     for (int j = 0; j < MaxArraySamples; j = j + steps) { 
      array[j] = dist(mt); 
     } 
    currentMyMatrix_.push_back(array); 
    } 
} 

トラブルはrankedMyMatrix_マップへの各プッシュで、新しいupdate()に、それは同じ行列を生成することです。それともそれに書いていないのですか?おそらく私はここに何かを見逃しているので、どんな助けもありがたいです。ここでUPDATE

はコンパイルおよびテストできるコードです:

#include <iostream> 
#include "MyClass.h" 

using namespace std; 

int main() { 

    MyClass* c; 
    c = new MyClass(); 

    int x = 0; 

    while (x<3){ 
     c->update(); 
     x++; 
    } 

    return 0; 
} 

ヘッダファイル:

#ifndef PROBA_MYCLASS_H 
#define PROBA_MYCLASS_H 

#include <vector> 
#include <map> 

typedef std::vector<double> MyArray; 
typedef std::vector <MyArray> MyMatrix; 

const int NoMotors = 4; 
const int MaxArraySamples = 10; 

class MyClass { 
public: 
    MyClass(); 

    ~MyClass(); 

    void update(); 

private: 
    std::map<double, MyMatrix, std::greater<double>> rankedMyMatrix_; 

    MyMatrix currentMyMatrix_; 
    int matrixRank_; 

    void generateMyMatrix(); 

    void writeCurrent(); 

}; 

#endif //PROBA_MYCLASS_H 

クラスファイル:

#include "MyClass.h" 

#include <random> 
#include <iostream> 
#include <fstream> 

MyClass::MyClass() { 
    currentMyMatrix_.reserve(NoMotors); 
    matrixRank_ = 0; 
} 

MyClass::~MyClass() { } 

void MyClass::update() { 

    this->generateMyMatrix(); // Generates new random currentMyMatrix_ 

    rankedMyMatrix_.insert({matrixRank_, currentMyMatrix_}); 
    matrixRank_++; 

    this->writeCurrent(); 
} 

void MyClass::generateMyMatrix() { 
    std::random_device rd; 
    std::mt19937 mt(rd()); 
    std::uniform_real_distribution<double> dist(-1, 1); 
    for (int i = 0; i < NoMotors; i++) { 
     MyArray array(MaxArraySamples, 0); 
     for (int j = 0; j < MaxArraySamples; j = j + 3) { 
      array[j] = dist(mt); 
     } 
     currentMyMatrix_.push_back(array); 
    } 
} 

void MyClass::writeCurrent() { 
    std::ofstream outputFile; 
    std::string uri = "/tmp/output/test"; 
    outputFile.open(uri + std::to_string(matrixRank_) + ".txt"); 
    for (int i = 0; i < MaxArraySamples; i++) { 
     for (int j = 0; j < NoMotors; j++) { 
      outputFile << currentMyMatrix_[j][i] << " "; 
     } 
     outputFile << std::endl; 
    } 
    outputFile.close(); 
} 

とCMakeLists.txt :

cmake_minimum_required(VERSION 3.5) 
project(proba) 

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") 

set(SOURCE_FILES main.cpp MyClass.h MyClass.cpp) 
add_executable(proba ${SOURCE_FILES}) 

それでも同じ出力が得られます。私は無知だ。

+0

:エラーがそれは常に最後に新しい値をプッシュ

currentMyMatrix_.push_back(array); 

を使用していましたので、コードは最初noMotors_ arrays.Thisを参照するソリューションですですあなたは本当にMinGWを使用していますか? –

+0

@ baum-mit-augenいいえ、Mac – miller

+0

あなたが使っているコンパイラと標準ライブラリのバージョンを教えてください。しかし、gcc/libstdC++の場合、おそらくMinGWの場合と同じ問題です。 –

答えて

0

まあ、私は最終的に問題を解決しました。私はこれをミョルニル前に念のために、

currentMyMatrix_.at(i) = array; 
1

ジェネレータをリセットしているため、同じランダムシーケンスの時間と時刻を再度生成する可能性があります。

http://en.cppreference.com/w/cpp/numeric/random/random_device

非決定論的ソース(例えば、ハードウェア装置)の実装に利用できない場合はstd :: random_deviceは実装定義擬似乱数エンジンの点で実施することができます。この場合、各std :: random_deviceオブジェクトは同じ番号シーケンスを生成することがあります。

関連する問題