C++中随机生成的矢量不会改变值
Random generated vector in C++ is not changing values
我的类包含一些类型为的向量矩阵
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;
相关文章:
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- 为什么 Serial.println(<char[]>);返回随机字符?
- 字符串-C++后显示的随机字符
- 变量没有改变?通过向量的函数调用
- 循环中的随机函数
- 在c++构造函数中使用随机字符串生成器
- 使用std::mt19937从字符串中返回一个随机单词
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- 如何在C++中高效地构造随机骰子
- 在类中使用随机生成器时出现性能问题
- 在将数字随机生成为数组期间从内存输出随机数的数组
- 如何改变c++应用程序的视觉效果
- C++在不同线程中改变向量
- 如何访问和改变存储在矢量C++中的对象
- 将字符随机转换为大写的函数
- 为什么 vector 的随机访问迭代器给出与指针不同的内存地址?
- 在C++行尾写一个分号或多个分号是否会改变任何内容
- 如何生成一个随机的 n 位数,其中 n 是任意的
- 将随机生成的数字添加到数组 + 对这些数组求平均值
- C++中随机生成的矢量不会改变值