C ++,mpi:创建随机数据的最简单方法 [英] C++ ,mpi : easiest way to create random data
本文介绍了C ++,mpi:创建随机数据的最简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的c ++ mpi项目中,我创建了函数:
In my c++ mpi project i created function:
RandomDataInitialization(pMatrix, pVector, Size);
,并且我试图在函数RandomDataInitialization中形成矩阵A和向量b的值.所以我想问问也许有人知道最简单,最有效的方法吗?
and I am trying to form the values for matrix A and vector b in function RandomDataInitialization. So i want to ask maybe someone knows the easiest and most effective way to do this ?
推荐答案
一般而言.
c ++标准随机函数的工作方式如下:
The way c++ standard random functions work is as follows:
- 创建一个伪随机引擎.
- 使用随机种子对其进行初始化(其中一个很好的来源是std :: random_device)
- 创建一个分发对象(例如Uniform_int_distribution或Unified_real_distribution)
- 通过分布对象传递生成的伪随机数(由引擎生成)以提供随机数.
例如,要随机化数组或向量(矩阵的一种可能的存储机制):
For example, to randomise an array or vector (a likely storage mechanism for your matrix):
#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 ++,mpi:创建随机数据的最简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文