C++ 中随机生成的向量不会改变值

Random generated vector in C++ is not changing values

我的 class 包含一些类型的向量矩阵:

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

Class 有一个 update() 方法,在每次调用时它应该生成一个新的随机矩阵,并在 std::map<double, MyMatrix, std::greater<double>> rankedMyMatrix_;.

中生成旧位置

更新方法大致如下所示:

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() 上它都会生成相同的矩阵。或者它不写信给它?可能我在这里遗漏了一些东西,所以我们将不胜感激。

更新

可以编译测试的代码如下:

#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

Class 文件:

#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})

它仍然给出相同的输出。我一头雾水

您可能会一次又一次地生成相同的随机序列,因为您正在重置生成器。

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

std::random_device may be implemented in terms of an implementation-defined pseudo-random number engine if a non-deterministic source (e.g. a hardware device) is not available to the implementation. In this case each std::random_device object may generate the same number sequence.

好吧,我终于解决了问题。使用

时出错
currentMyMatrix_.push_back(array);

它总是在末尾推送新值,因此代码只会首先看到 noMotors_ arrays.This 是一个解决方案:

currentMyMatrix_.at(i) = array;