gpt4 book ai didi

c++ - C++ 中随机生成的 vector 不会改变值

转载 作者:行者123 更新时间:2023-11-28 05:44:22 25 4
gpt4 key购买 nike

我的类包含一些类型的 vector 矩阵:

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_ 时 map ,新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 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.

关于c++ - C++ 中随机生成的 vector 不会改变值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36510297/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com