C++,mpi:创建随机数据的最简单方法
C++ ,mpi : easiest way to create random data
在我的c++mpi项目中,我创建了函数:
RandomDataInitialization(pMatrix, pVector, Size);
并且我试图在函数RandomDataInitialization中形成矩阵A和向量b的值。所以我想问,也许有人知道最简单、最有效的方法?
一般情况下。
c++标准随机函数的工作方式如下:
- 创建一个伪随机引擎
- 用一个随机种子初始化它(std::random_device是一个很好的来源)
- 创建分布对象(例如uniform_int_distribution或uniform_real_distribution)
- 通过分布对象传递生成的伪随机数(由引擎生成)以给出随机数
例如,要随机化数组或向量(可能是矩阵的存储机制):
#include <random>
#include <array>
#include <algorithm>
int main()
{
// a 3x3 matrix of doubles
std::array<double, 9> matrix_data;
// make an instance of a random device to generate one real random number
// this is "slow" so we do it as little as possible.
std::random_device rd {};
// create the random engine and seed it from the random device
auto engine = std::default_random_engine(rd());
// create a uniform distribution generator which gives values in the range
// 0.0 to 1.0
auto distribution = std::uniform_real_distribution<double>(0, 1.0);
// generate the random data by passing random numbers generated by the
// engine through the distribution object.
std::generate(std::begin(matrix_data), std::end(matrix_data),
[&distribution, &engine]
{
return distribution(engine);
});
}
相关文章:
- 当无法使用模板和宏时,生成类型变体C++代码的最简单方法是什么?
- 在OSX上使用CMake将Adobe的XMP工具包构建为共享库的最简单方法是什么?
- 通过比较C++中的行在 txt 文件中搜索的最简单方法是什么?
- 请求最简单的 OpenMP 目标 GPU 示例
- 将时间戳打印到流的最简单方法
- 在C++中创建文件夹选取器对话框的最简单方法是什么?
- C++20 个模块在 clang (Windows):在最简单的示例中键入信息错误
- 对多个(可能)重叠范围进行分区的最简单算法
- 在 cpp 中最简单的图形实现
- 从 txt 文件中读取数据的最简单方法
- C++用户将数据输入 2D 数组的最简单方法是什么?
- 比较来自同一类型的两个对象的数据成员的最简单方法
- 在QTcpSocket中连续发送XML数据的最简单方法
- C++,mpi:创建随机数据的最简单方法
- 创建 2D 数组并在每个数据槽中填充不同字符的最简单方法是什么?
- 在c++中读取格式化数据的最简单/最清晰的方法
- 从文件加载"irregular"数据的最简单方法 C++
- C++11 在map<key,value>中存储值(整数和字符串)的多种数据类型的最简单方法?
- 解析此数据的最简单方法是什么?
- 从 std::vector<unsigned char> 读取二进制数据的最简单方法?