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

Random generated vector in C++ is not changing values

本文关键字:改变 随机 C++      更新时间:2023-10-16

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

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

类有一个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

类文件:

#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。在这种情况下,每个std::random_device对象可以生成相同的数字序列。

好吧,我终于解决了这个问题。使用时出错

currentMyMatrix_.push_back(array);

它总是在末尾推送新值,所以代码只能看到第一个noMotors_数组。这是一个解决方案:

currentMyMatrix_.at(i) = array;