C++math库里有生成高斯分布随机数的函数么?

作者&投稿:经烁 (若有异议请与网页底部的电邮联系)
Matlab怎么样利用计算机内部函数产生高斯分布的随机数呢?~

可调用matlab内部函数,格式为u=randn(N) 或者u=randn(M,N),前者为N维向量,后者为M*N矩阵,
这种调用方式产生的随机序列均值为零,方差为1
例如:R = normrnd(Mu, Sigma, m, n)
%产生服从N(Mu, Sigma^2) 分布的m行n列的随机数组R

如何生成正态分布随机数marsaglia和bray
java.util
类 Random
java.lang.Object
java.util.Random
所有已实现的接口:
Serializable
直接已知子类:
SecureRandom

VC2008 FeturePack1 以后有,参见http://growupsoft.blog.163.com/blog/static/960729200903104720643/
对于可以解析表达成C++的特殊概率分布,根据概率论的原理,产生均匀分布的随机分布,而后代入分布函数就可以产生高斯分布的随机数了阿.下面原版转载:
At the core of any pseudorandom number generation software is a routine for generating uniformly distributed random integers.
In C++ TR1 you have your choice of several core generators that it calls “engines.” The following four engine classes are supported in the Visual Studio 2008 feature pack

(微软已经发布了Visual Studio 2008的Services Pack 1,它包含了此前发布的feature pack,以及完整的TR1支持,后来又发布了一个修正:VC 2008 SP1: Problems with STL/TR1 after installing VS2008 SP1,关于:VC9 SP1 Hotfix For The vector<function<FT>> Crash,关于文档:微软TR1文档).

linear_congruential uses a recurrence of the form x(i) = (A * x(i-1) + C) mod M
mersenne_twister implements the famous Mersenne Twister algorithm
subtract_with_carry uses a recurrence of the form x(i) = (x(i - R) - x(i - S) - cy(i - 1)) mod M in integer arithmetic
subract_with_carry_01 uses a recurrence of the form x(i) = (x(i - R) - x(i - S) - cy(i - 1)) mod 1 in floating point arithmetic
Each engine has a seed() method that accepts an unsigned long argument to specify the random number generation seed. It is also possible to set the seed in more detail using template parameters unique to each engine.

微软的C++ TR1可以生成以下分布的随机数:

Generates a Bernoulli distribution.
Generates a binomial distribution.
Generates an exponential distribution.
Generates a gamma distribution.
Generates a geometric distribution.
Generates a normal distribution.
Generates a Poisson distribution.
Generates a uniform integer distribution.
Generates a uniform floating-point distribution.

试验一:在VS2008中先建一空的VC++项目文件,然后添加新建项cpp文件如下:

#include <random>
#include <iostream>

void main(){
std::tr1::mt19937 eng; // a core engine class:Mersenne Twister generator
std::tr1::normal_distribution<double> dist;
std::tr1::uniform_int<int> unif(1, 52);

for (int i = 0; i < 10; ++i) //产生正态分布的10个随机数
std::cout << dist(eng)<<std::endl;

for(int i = 0; i < 5; ++i) //产生均匀分布的在1到52之间的五个整数随机数
std::cout << unif(eng) << std::endl;
}

在循环中,每循环一次,就调用mt19937 eng一次,产生一个随机数输出。

试验二:关于种子seed

#include <random>
#include <iostream>
#include <time.h>

void main(){
std::tr1::mt19937 eng; // a core engine class:Mersenne Twister generator
std::tr1::normal_distribution<double> dist;
std::tr1::uniform_int<int> unif(1, 52);
eng.seed((unsigned int)time(NULL)); // reseed base engine 设置种子用#include <time.h>, 不能用#include <time>

for (int i = 0; i < 10; ++i) //产生正态分布的10个随机数
std::cout << dist(eng)<<std::endl;
//eng.seed(); // reseed base engine
for(int i = 0; i < 5; ++i) //产生均匀分布的在1到52之间的五个整数随机数
std::cout << unif(eng) << std::endl;
}

试验三:随机数写入文件

#include <random>
#include <iostream>
#include <fstream>
#include <time.h>

using namespace std;
using namespace std::tr1;

void main()
{
mt19937 eng; // a core engine class:Mersenne Twister generator
normal_distribution<double> dist;
uniform_int<int> unif(1, 52);
eng.seed((unsigned int)time(NULL)); // 设置种子用#include <time.h>, 不能用#include <time>

for (int i = 0; i < 10; ++i) //产生正态分布的10个随机数
cout << dist(eng)<<endl;

ofstream fileout("fileout.dat");
for(int i = 0; i < 5; ++i) //产生均匀分布的在1到52之间的五个整数随机数
fileout << unif(eng)<< endl;

fileout.close();
}

试验四:第三方"Mersenne Twister"随机数生成程序使用试验(程序来源:Agner Fog http://www.agner.org/random/)

// 使用说明:从网站下载压缩包,http://www.agner.org/random/randomc.zip
// 展开后,将其中的randomc.h头文件及mersenne.cpp文件Copy到项目文件夹,
// 并将它们加入到项目中,其中包括"Mersenne Twister"的实现

#include <iostream>
#include <time.h>
#include "randomc.h" // define classes for random number generators

using namespace std;

void main()
{

int seed = (int)time(0); // random seed

// choose one of the random number generators:
CRandomMersenne RanGen(seed); // make instance of random number generator
cout<<"\n\nRandom integers in interval from 0 to 99:\n";
for (int i = 0; i < 40; i++) {
int ir = RanGen.IRandom(0,99);
cout<<ir<<" ";
}

cout <<endl;

cout<<"\n\n\n\nRandom floating point numbers in interval from 0 to 1:\n";
for (int i = 0; i < 40; i++) {
float fr = RanGen.Random();
cout<<fr<<" ";
}

cout <<endl;
}

试验五:第三方"Mother-Of-All"随机数生成程序使用试验(程序来源:Agner Fog http://www.agner.org/random/)

// 使用说明:从网站下载压缩包,http://www.agner.org/random/randomc.zip
// 展开后,将其中的randomc.h头文件及mother.cpp文件Copy到项目文件夹,
// 并将它们加入到项目中,其中包括"Mother-Of-All" generator invented by George Marsaglia 的实现

#include <iostream>
#include <time.h>
#include "randomc.h" // define classes for random number generators

using namespace std;

void main()
{

int seed = (int)time(0); // random seed

// choose one of the random number generators:
CRandomMother RanGen(seed); // make instance of random number generator
cout<<"\n\nRandom integers in interval from 0 to 99:\n";
for (int i = 0; i < 40; i++) {
int ir = RanGen.IRandom(0,99);
cout<<ir<<" ";
}

cout <<endl;

cout<<"\n\n\n\nRandom floating point numbers in interval from 0 to 1:\n";
for (int i = 0; i < 40; i++) {
float fr = RanGen.Random();
cout<<fr<<" ";
}

cout <<endl;
}

试验六:第三方"SFMT"随机数生成程序使用试验(程序来源:Agner Fog http://www.agner.org/random/)

重要提示:在编译前,可以修改头文件sfmt.h中的#define MEXP以及下面相应的宏代码:Choose one of the possible Mersenne exponents. Higher values give longer cycle length and use more memory。SFMT利用了SSE2指令,速度最快,但只适合intel系列的部分芯片。

// 使用说明:从网站下载压缩包,http://www.agner.org/random/randomc.zip
// 展开后,将其中的头文件及sfmt.cpp文件Copy到项目文件夹,
// 并将它们加入到项目中,其中包括"SFMT" 的实现

#include <iostream>
#include <time.h>
#include "sfmt.h" // define classes for random number generators

using namespace std;

void main()
{

int seed = (int)time(0); // random seed

// choose one of the random number generators:
CRandomSFMT1 RanGen(seed); //注意可以是CRandomSFMT,是CRandomSFMT0,或CRandomSFMT1
cout<<"\n\nRandom integers in interval from 0 to 99:\n";
for (int i = 0; i < 40; i++) {
int ir = RanGen.IRandomX(0,99);
cout<<ir<<" ";
}

cout <<endl;

cout<<"\n\n\n\nRandom floating point numbers in interval from 0 to 1:\n";
for (int i = 0; i < 40; i++) {
float fr = RanGen.Random();
cout<<fr<<" ";
}

cout <<endl;
}


下列属于math库的有()
下列属于math库的有()A.sin B.pi C.cos D.exp 正确答案:ABCD

math库中floor函数的功能
math库中的floor函数用于返回不大于输入参数的最大整数,即向下取整。math库是Python中用于数学计算的标准库,提供了多种数学相关的函数。其中的floor函数非常实用,它可以将一个浮点数向下取整为最接近的整数。具体来说,对于给定的输入数值,floor函数会返回不大于该数值的最大整数。如果输入本身就是一个...

常用的math类
常用的数学类包括以下几个:1. `math`模块:这是Python标准库中的一个模块,提供了大量的数学函数和常量。这些函数和常量可以帮助你进行各种数学计算,包括三角函数、对数函数、指数函数等。例如,你可以使用`math.sqrt()`来计算一个数的平方根,使用`math.exp()`来计算自然对数等。2. `numpy`模块:...

# include< math. h>什么意思?包含了什么?
include<math.h> 意思是包含math库,实际上就是一个头文件,里面是一些已经写好的代码,形式上是一个个的函数,包含进来以后就可以使用里面的各种数学函数,如幂函数、三角函数、指数函数等。

math库函数都有什么
include <math.h> double sin(double arg);返回arg的正弦值,arg单位为弧度。4.2 cos include <math.h> double cos(double arg);返回arg的余弦值,arg单位为弧度。4.3 tan include <math.h> double tan(double arg);返回arg的正切值,arg单位为弧度。4.4 asin include <math.h> double ...

小白学习python之路第八篇——math库
除此之外你需要计算复数,可以使用 cmath 模块中的同名函数。我们先查看一下math库里面有哪些函数:我们可以先罗列math库里面的函数。如下表:由列表中可以发现,math库主要包含为三种类型:一是魔法函数,二是math静态参数(上表中标黄部分),三是各类函数。其中,函数主要为三角函数,反三角函数等各类...

c语言中math头文件中的函数有哪些
数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有:1 三角函数 double sin (double);double cos (double);double tan (double);2 反三角函数 double asin (double); 结果介于[-PI\/2, PI\/2]double acos (double); 结果介于[0, PI]double atan (double); 反正切(主值), 结果...

math.h的所包含的函数
数学函数库,一些数学计算的公式的具体实现是放在math.h里,具体有:1、 三角函数double sin(double);正弦double cos(double);余弦double tan(double);正切2 、反三角函数double asin (double); 结果介于[-PI\/2,PI\/2]double acos (double); 结果介于[0,PI]double atan (double); 反正切(主值)...

math库是第三方库吗
math库是第三方库math库是python的基本库,也是第三方库。Python的math库是调用了C语言底层的math库,使用方法基本一样,其中常见包括sin、cos、log(对数)、pow(指数)asin(反三角函数)等等。

math是什么意思计算机
Math库作为计算机编程中数学计算的基础,是各种软件和应用程序的重要组成部分。在图形处理、物理模拟、统计分析、金融计算、科学计算等各个领域中,Math库都扮演着极其重要的角色。同时,Math库也给人们的工作和学习带来了很大的便利,使得计算机的数学处理变得更加高效和准确。Math库不仅在计算机编程中有着广泛...

平乡县15765759363: C++math库里有生成高斯分布随机数的函数么? -
驹房至宝: VC2008 FeturePack1 以后有,参见http://growupsoft.blog.163.com/blog/static/960729200903104720643/ 对于可以解析表达成C++的特殊概率分布,根据概率论的原理,产生均匀分布的随机分布,而后代入分布函数就可以产生高斯分布的随机数...

平乡县15765759363: matlab 中的randn(m,n)生成m*n的高斯随机矩阵的函数怎样用C/C++语言编程实现啊?求大虾赐教. -
驹房至宝: #include double uniform(double a, double b, long int *seed)//a,b 分别是均匀分布数的上下限,*seed是随机数的种子 { double t; *seed = 2045 * (*seed) + 1; *seed = *seed - (*seed / 1048576) * 1048576; t = (*seed) / 1048576.0; t = a + (b - a) * t; ...

平乡县15765759363: 求一个VC++程序,产生高斯分布的随机数 -
驹房至宝: 算出高斯分布的随机数函数为 f 则 下面生成要求的函数 g=f*2-1

平乡县15765759363: 如何对c++库函数中的正态分布函数设置初始化种子 -
驹房至宝: double gaussian(double u) //用Box_Muller算法产生高斯分布的随机数 { double r,t,z,x; double s1,s2; s1=(1.0+rand())/(RAND_MAX+1.0); s2=(1.0+rand())/(RAND_MAX+1.0); r=sqrt(-2*log(s2)/log(e)); t=2*pi*s1; z=r*cos(t); x=u+z*N; return x; }

平乡县15765759363: C++工具中,随机数是怎样产生的?Mathlab呢??Java工具呢?原理一样吗?? -
驹房至宝: C++一般采用和系统时间搭配来产生随机数 经典的《c程序设计教程》是这样做的#indclude"time.h"srand(time(NULL));int x;x=rand(); 楼主说的是matlab 吧 matlab产生随机数的方法有很多,因为matlab专门是用来做数值计算的 randon ...

平乡县15765759363: matlab怎么生成随机的高斯分布点? -
驹房至宝: 分别生成XY坐标就可以. 蓝色点 B = 3.5+randn(100, 2); 红色点 R = randn(100, 2); 得到的是100*2的矩阵,每行是一个坐标

平乡县15765759363: 怎样产生标准分布或高斯分布的随机数? -
驹房至宝: 这里有一个由 Marsaglia 首创 Knuth 推荐的方法: #include <stdlib.h> #include <math.h> double gaussrand() { static double V1, V2, S; static int phase = 0; double X; if(phase == 0) { do { double U1 = (double)rand() / RAND_MAX; double U2 = (...

平乡县15765759363: 用C语言实现瑞利分布,莱斯分布,高斯分布的分布函数 -
驹房至宝: C语言中的random函数可以产生均匀分布的随机变量分布区间为(0,1),假设x1,x2是由random产生的随机变量,则y=sqrt(-2*ln(x1))为瑞利分布 theta=2*pi*x2为(0,2*pi)的均匀分布 n1=y*cos(theta),n2=y*sin(theta)为两个独立的正太分布 z=sqrt((a+n1)^2+(b+n2)^2),为莱斯分布,a ,b为常数

平乡县15765759363: matlab中如何生成一个随机信号 -
驹房至宝: 原发布者:李世民的111Matlab中随机信号的产生在matlab编程中,我们所能用到的用于产生随机信号的函数有三:Rand,randn,randi下面我们详细的了解一下这三个函数.1.Rand功能是生产均匀分布的伪随机数,并且所生成的伪随机数分布在...

平乡县15765759363: 如何产生复高斯分布随机变量,比如我要产生2248个这样的随机变量,用matlab怎么编 -
驹房至宝: function sim() ux=1; dx=10; uy=1; dy=10; x=sqrt(dx)*(randn(1,2248)+ux); y=sqrt(dy)*(randn(1,2248)+uy); z=x+i*y;

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 星空见康网