资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

高斯分布c语言函数 c++ 高斯分布

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

VC2008 FeturePack1 以后有,参见

创新互联拥有一支富有激情的企业网站制作团队,在互联网网站建设行业深耕十余年,专业且经验丰富。十余年网站优化营销经验,我们已为数千家中小企业提供了网站设计制作、做网站解决方案,按需定制开发,设计满意,售后服务无忧。所有客户皆提供一年免费网站维护!

对于可以解析表达成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 vectorfunctionFT 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_distributiondouble dist;

std::tr1::uniform_intint 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_distributiondouble dist;

std::tr1::uniform_intint 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_distributiondouble dist;

uniform_intint 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 )

// 使用说明:从网站下载压缩包,

// 展开后,将其中的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);

coutir" ";

}

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();

coutfr" ";

}

cout endl;

}

试验五:第三方"Mother-Of-All"随机数生成程序使用试验(程序来源:Agner Fog )

// 使用说明:从网站下载压缩包,

// 展开后,将其中的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);

coutir" ";

}

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();

coutfr" ";

}

cout endl;

}

试验六:第三方"SFMT"随机数生成程序使用试验(程序来源:Agner Fog )

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

// 使用说明:从网站下载压缩包,

// 展开后,将其中的头文件及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);

coutir" ";

}

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();

coutfr" ";

}

cout endl;

}

用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为常数

怎么调用c语言中的标准正态分布函数?

摘要:

随机数在实际运用中非常之多,如游戏设计,信号处理,通常我们很容易得到平均分布的随机数。但如何根据平均分布的随机数进而产生其它分布的随机数呢?本文提出了一种基于几何直观面积的方法,以正态分布随机数的产生为例讨论了任意分布的随机数的产生方法。

大家都知道,随机数在各个方面都有很大的作用,在vc的环境下,为我们提供了库函数rand()来产生一个随机的整数。该随机数是平均在0~RAND_MAX之间平均分布的,RAND_MAX是一个常量,在VC6.0环境下是这样定义的:

#define RAND_MAX 0x7fff

它是一个short 型数据的最大值,如果要产生一个浮点型的随机数,可以将rand()/1000.0这样就得到一个0~32.767之间平均分布的随机浮点数。如果要使得范围大一点,那么可以通过产生几个随机数的线性组合来实现任意范围内的平均分布的随机数。例如要产生-1000~1000之间的精度为四位小数的平均分布的随机数可以这样来实现。先产生一个0到10000之间的随机整数。方法如下 :

int a = rand()%10000;

然后保留四位小数产生0~1之间的随机小数:

double b = (double)a/10000.0;

然后通过线性组合就可以实现任意范围内的随机数的产生,要实现-1000~1000内的平均分布的随机数可以这样做:

double dValue = (rand()%10000)/10000.0*1000-(rand()%10000)/10000.0*1000;

则dValue就是所要的值。

到现在为止,你或许以为一切工作都已经完成了,其实不然,仔细一看,你会发现有问题的,上面的式子化简后就变为:

double dValue = (rand()%10000)/10.0-(rand()%10000)/10.0;

这样一来,产生的随机数范围是正确的,但是精度不正确了,变成了只有一位正确的小数的随机数了,后面三位的小数都是零,显然不是我们要求的,什么原因呢,又怎么办呢。

先找原因,rand()产生的随机数分辨率为32767,两个就是65534,而经过求余后分辨度还要减小为10000,两个就是20000而要求的分辨率为1000*10000*2=20000000,显然远远不够。下面提供的方法可以实现正确的结果:

double a = (rand()%10000) * (rand()%1000)/10000.0;

double b = (rand()%10000) * (rand()%1000)/10000.0;

double dValue = a-b;

则dValue就是所要求的结果。在下面的函数中可以实现产生一个在一个区间之内的平均分布的随机数,精度是4位小数。

double AverageRandom(double min,double max)

{

int minInteger = (int)(min*10000);

int maxInteger = (int)(max*10000);

int randInteger = rand()*rand();

int diffInteger = maxInteger - minInteger;

int resultInteger = randInteger % diffInteger + minInteger;

return resultInteger/10000.0;

}

但是有一个值得注意的问题,随机数的产生需要有一个随机的种子,因为用计算机产生的随机数是通过递推的方法得来的,必须有一个初始值,也就是通常所说的随机种子,如果不对随机种子进行初始化,那么计算机有一个确省的随机种子,这样每次递推的结果就完全相同了,因此需要在每次程序运行时对随机种子进行初始化,在vc中的方法是调用srand(int)这个函数,其参数就是随机种子,但是如果给一个常量,则得到的随机序列就完全相同了,因此可以使用系统的时间来作为随机种子,因为系统时间可以保证它的随机性。

调用方法是srand(GetTickCount()),但是又不能在每次调用rand()的时候都用srand(GetTickCount())来初始化,因为现在计算机运行时间比较快,当连续调用rand()时,系统的时间还没有更新,所以得到的随机种子在一段时间内是完全相同的,因此一般只在进行一次大批随机数产生之前进行一次随机种子的初始化。下面的代码产生了400个在-1~1之间的平均分布的随机数。

double dValue[400];

srand(GetTickCount());

for(int i= 0;i 400; i++)

{

double dValue[i] = AverageRandom(-1,1);

}

c语言怎么产生n维高斯分布数据

function relation(a,b,n)

!本程序计算两列向量的相关系数

!a,b分别是待计算的向量

!n是向量的长度,要求两列向量等长

implicit none

integer,intent(in)::n

real,intent(in)::a(n),b(n)

real::relation !返回的相关系数

integer::i,j !循环控制变量

real::sfenzi,sfenmu1,sfenmu2,s !加法器

real::amean,bmean !a,b向量的平均值

!计算平均值

s=0.

do i=1,n

s=s+a(i)

end do

amean=s/n

s=0.

do i=1,n

s=s+b(i)

end do

bmean=s/n

!计算相关系数

sfenzi=0.

sfenmu1=0.

sfenmu2=0.

do i=1,n

sfenzi=sfenzi+(a(i)-amean)*(b(i)-bmean)

sfenmu1=sfenmu1+(a(i)-amean)**2

sfenmu2=sfenmu2+(b(i)-bmean)**2

end do

relation=sfenzi/sqrt(sfenmu1*sfenmu2)

end function relation


分享名称:高斯分布c语言函数 c++ 高斯分布
分享地址:http://cdkjz.cn/article/ddeespe.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220