当前位置:首页 » 编程语言 » 高斯随机分布c语言实现
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

高斯随机分布c语言实现

发布时间: 2023-05-30 13:48:54

‘壹’ 如何产生满足高斯分布的随机数据

这篇?戚孙:
就这个就可神弯以了,不用改进法

正态分布的随机数发生器 in C#
主要参考《Numerical Recipes in C++ 2/e》p.292~p.294 和《Simulation Modeling and Analysis
3/e》p.465~p.466。

Box 和 Muller 在 1958 年给出了由均匀分布的随机变量生成正态分布的随机变量的算法。设 U1, U2 是区间 (0, 1) 上均匀分布的随机变量,且相互独立。令

X1 = sqrt(-2*log(U1)) * cos(2*PI*U2);
X2 = sqrt(-2*log(U1)) * sin(2*PI*U2);

那么 X1, X2 服从游仔闷 N(0,1) 分布,且相互独立。等于说我们用两个独立的 U(0,1) 随机数得到了两个独立的 N(0,1)随机数。

‘贰’ 怎样产生标准分布或高斯分布的随机数

这里有一亮敬判个由 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 = (double)rand() / RAND_MAX;
V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);
X = V1 * sqrt(-2 * log(S) / S);
} else
X = V2 * sqrt(-2 * log(S) / S);
phase = 1 - phase;
return X;
}

‘叁’ 高斯变换,在c语言中实现的思路,知道的请告诉我,详细点越好

#include <stdio.h>
#include <iostream.h>
#include <math.h>
#include<iomanip.h>
class Angle
{
public:
double degree,cent,second,Hu,seconds;
//
构造函数
Angle(double _degree,double _cent,double _second)
{
degree=_degree;
cent=_cent;
second=_second;
seconds=(degree*3600+cent*60+second);
Hu=(degree*3600+cent*60+second)*2*3.1415926535/(360*60*60);
}
Angle()
{
}
SToD()
{
second=seconds-int(seconds/60)*60;
degree=int((seconds-second)/3600);
cent=int((seconds-second-degree*3600)/60);
}
~Angle()
{
}
};
void main()
{
//
正算过程
//
定义变量
Angle B(51,38,43.9023),L(126,2,13.1360),_B,_L;
double L0,l,N,a0,a4,a6,a3,a5,x,y,rou;
rou=(360*60*60)/(2*3.1415926535);
if(int(L.degree)%6!=0)
{
L0=6*(int(L.degree)/6+1)-3+6;
}
else
L0=6*(int(L.degree)/6)-3+6;
l=L.Hu-L0*3600*2*3.1415926535/(360*60*60);
N=6399698.902-(21562.267-(108.973-0.612*cos(B.Hu)*cos(B.Hu))*cos(B.Hu)*cos(
B.Hu))*cos(B.Hu)*cos(B.Hu);
a0=32140.404-(135.3302-(0.7092-0.0040*cos(B.Hu)*cos(B.Hu))*cos(B.Hu)*cos(B.
Hu))*cos(B.Hu)*cos(B.Hu);
a4=(0.25+0.00252*cos(B.Hu)*cos(B.Hu))*cos(B.Hu)*cos(B.Hu)-0.04166;
a6=(0.166*cos(B.Hu)*cos(B.Hu)-0.084)*cos(B.Hu)*cos(B.Hu);
a3=(0.3333333+0.001123*cos(B.Hu)*cos(B.Hu))*cos(B.Hu)*cos(B.Hu)-0.166666
7;
a5=0.0083-(0.1667-(0.1968+0.0040*cos(B.Hu)*cos(B.Hu))*cos(B.Hu)*cos(B.Hu)
)*cos(B.Hu)*cos(B.Hu);
x=6367558.4969*B.Hu-(a0-(0.5+(a4+a6*l*l)*l*l)*l*l*N)*sin(B.Hu)*cos(B.Hu);
y=(1+(a3+a5*l*l)*l*l)*l*N*cos(B.Hu);
//结果输出
cout<银纯猛<"x="<<x<<endl;
cout<锋桥<"y="<裤哪<y<<endl;
// 反算过程
double b,Bf,Nf,Z,b2,b3,b4,b5,l1;
b=(x/6367558.4969);
Bf=b+(50221746+(293622+(2350+22*cos(b)*cos(b))*cos(b)*cos(b))*cos(b)*cos(b))*0.000
0000001*sin(b)*cos(b);
Nf=6399698.902-(21562.267-(108.973-0.612*cos(Bf)*cos(Bf))*cos(Bf)*cos(Bf))*cos(Bf)*c
os(Bf);
Z=y/(Nf*cos(Bf));
b2=(0.5+0.003369*cos(Bf)*cos(Bf))*sin(Bf)*cos(Bf);
b3=0.333333-(0.166667-0.001123*cos(Bf)*cos(Bf))*cos(Bf)*cos(Bf);
b4=0.25+(0.16161+0.00562*cos(Bf)*cos(Bf))*cos(Bf)*cos(Bf);
b5=0.2-(0.1667-0.0088*cos(Bf)*cos(Bf))*cos(Bf)*cos(Bf);
_B.seconds=Bf*rou-(1-(b4-0.12*Z*Z)*Z*Z)*Z*Z*b2*rou;
l1=(1-(b3-b5*Z*Z)*Z*Z)*Z*rou;
_L.seconds=L0*3600+l1;
_B.SToD();
_L.SToD();

‘肆’ 对于matlab中的函数norminv,C语言中有没有函数可以完成类似的功能没有的话,用C语言要如何实现呢

C中都是最基础的函数,这样的函数是没有的!就连sum这样的都没有

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

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;
}

‘陆’ 标准正态分布函数的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;
}
以前写的一个函数,u是均值,N是方差

‘柒’ 用C语言实现瑞利分布,莱斯分布,高斯分布的分布函数

下面共有两个程序,程序2 加入了图形显示

程序1
这个程序就是你要的。
# include "stdio.h"
# include "math.h"
# include "stdlib.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000 /*这个值为N可以定义的最大长度*/
# define N 100 /*产生随机序列的点数,注意不要大于MAX_N*/

/*产生均匀分布的随机变量*/
void randa(float *x,int num);

/*产生瑞利分布的随机变量*/
void randr(float *x,int num);

/*产生标准高斯分布的随机变量*/
void randn(float *x,int num);

/*产生莱斯分布的随机变量*/
void randl(float *x, float a, float b, int num);

void fshow(char *name,float *x,int num);

main()
{
float x[N];
int i;
/*
randa(&x,N);
randr(&x,N);
randl(&x,10,10,N);

*/
randn(&x,N);
/*此时x[N]就是所需要的高斯分布的序列*/

/*显示该序列*/
fshow("x",&x,N);

getch();

}

void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x[i]=rand();
x[i]=x[i]/32768;
}
}

void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x1[i]=rand();
x[i]=x1[i]/32768;
x[i]=sqrt(-2*log(x[i]));
}

}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
}

}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
}

}

void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("\n");
printf(name);
printf(" = ");
L=6;
/*按照每行6个数据的格式显示*/
for(i=0;i<num;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==0) printf("\n");
if(x[i]>0) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}

程序 2
以下程序加入了图形显示的效果,因此更加直观,你可以参考一下。

/* 作者 Leo_nanjing
时间 2008.5.10
功能 生成各种分布的随机变量,并显示
*/

# include "stdio.h"
# include "math.h"
# include "graphics.h"
# include "math.h"
# include "dos.h"
# define MAX_N 3000
# define N 1000

void randa(float *x,int num);
void randr(float *x,int num);
void randn(float *x,int num);
void randl(float *x, float a, float b, int num);
void fshow(char *name,float *x,int num);

/*用于图形显示的部分*/
void init_graphic(unsigned color);
void plotxy(float *x, float *y, int num,int mode);
void plot(float *y,int num, int mode);
float max(float *x, int num);
float min(float *x, int num);
/*画出该随机序列的分布函数曲线*/
void plotpdf(float *x,int num,int part,int mode);

main()
{
float x[N];
int i;
randn(&x,N);
fshow("x",&x,N);
getch();

/*以下为图形显示部分*/
init_graphic(0);
/*显示随机序列*/
plot(&x,N,1);
getch();
/*显示其分布函数*/
plotpdf(&x,N,20,0);
getch();
}

void randa(float *x,int num)
{
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x[i]=rand();
x[i]=x[i]/32768;
}
}

void randr(float *x,int num)
{
float x1[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x1[i]=rand();
x[i]=x1[i]/32768;
x[i]=sqrt(-2*log(x[i]));
}

}
void randn(float *x,int num)
{
float x1[MAX_N],x2[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
}

}
void randl(float *x, float a, float b, int num)
{
float x1[MAX_N],x2[MAX_N];
float temp[MAX_N];
int i;
struct time stime;
unsigned seed;
gettime(&stime);
seed=stime.ti_hund*stime.ti_min*stime.ti_hour;
srand(seed);
for(i=0;i<num;i++)
{
x1[i]=rand();
x2[i]=rand();
x1[i]=x1[i]/32768;
x2[i]=x2[i]/32768;
temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI);
x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI);
x1[i]=temp[i];
x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i]));
}

}

void fshow(char *name,float *x,int num)
{
int i,sign,L;
float temp;
printf("\n");
printf(name);
printf(" = ");
L=6;
for(i=0;i<num;i++)
{
temp=i/L;
sign=temp;
if((i-sign*L)==0) printf("\n");
if(x[i]>0) printf(" %f ",x[i]);
else printf("%f ",x[i]);
}
}

/*以下为图形显示的函数*/
void init_graphic(unsigned color)
{
int graphicdriver,graphicmode;
graphicdriver=DETECT;
graphicmode=1;
initgraph(&graphicdriver,&graphicmode,"E:\\turboc2\\");
setbkcolor(color);
}

void plotxy(float *x, float*y, int num,int mode)
{
int i;
float max_x,max_y,min_x,min_y;
float x0,y0,x1,y1;
clrscr(0);
cleardevice();
setbkcolor(0);
max_x=max(x,num);
max_y=max(y,num);
min_x=min(x,num);
min_y=min(y,num);
setlinestyle(0,2,1);
line(65,35,65,445);
line(65,445,575,445);
setlinestyle(3,0,1);
line(65,35,575,35);
line(575,35,575,445);
setlinestyle(0,2,1);
if(max_x==min_x)
x0=320;
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y0=240;
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x0,y0,2);
for(i=1;i<num;i++)
{
if(max_x==min_x)
x1=320;
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y1=240;
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x1,y1,2);
line(x0,y0,x1,y1);
x0=x1;y0=y1;
}
printf("\n\n");
printf("%f",max_y);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\n");
printf("%f",(max_y+min_y)/2);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n");
printf("%f",min_y);
printf("\n %f",min_x);
printf(" ");
printf("%f",(max_x+min_x)/2);
printf(" ");
printf("%f",max_x);

}

void plot(float*y, int num,int mode)
{
int i;
float max_x,max_y,min_x,min_y;
float x0,y0,x1,y1;
float x[MAX_N];
clrscr(0);
cleardevice();
setbkcolor(0);
for(i=0;i<num;i++) x[i]=i+1;
max_x=max(x,num);
max_y=max(y,num);
min_x=min(x,num);
min_y=min(y,num);
setlinestyle(0,2,1);
line(65,35,65,445);
line(65,445,575,445);
setlinestyle(3,0,1);
line(65,35,575,35);
line(575,35,575,445);
setlinestyle(0,2,1);
if(max_x==min_x)
x0=320;
else
x0=(x[0]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y0=240;
else
y0=480-((y[0]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x0,y0,2);
for(i=1;i<num;i++)
{
if(max_x==min_x)
x1=320;
else
x1=(x[i]-min_x)*500/(max_x-min_x)+70;
if(max_y==min_y)
y1=240;
else
y1=480-((y[i]-min_y)*400/(max_y-min_y)+40);
if(mode==0) circle(x1,y1,2);
line(x0,y0,x1,y1);
x0=x1;y0=y1;
}
printf("\n\n");
printf("%f",max_y);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n\n");
printf("%f",(max_y+min_y)/2);
printf("\n\n\n\n\n\n\n\n\n\n");
printf("\n\n");
printf("%f",min_y);
printf("\n %f",min_x);
printf(" ");
printf("%f",(max_x+min_x)/2);
printf(" ");
printf("%f",max_x);

}

void plotpdf(float *x,int num,int part,int mode)
{
int i,j;
float max_x,min_x,round,deltax,up,down,sum;
float xl[MAX_N],yl[MAX_N];
sum=0;
max_x=max(x,num);
min_x=min(x,num);
round=max_x-min_x;
deltax=round/part;
xl[0]=min_x;
for(i=1;i<=part;i++)
{
xl[i]=min_x+deltax*i;
yl[i-1]=0;
up=xl[i];
down=xl[i-1];
for(j=0;j<num;j++)
{
if((x[j]<up) && (x[j]>=down)) yl[i-1]=yl[i-1]+1;

}
yl[i-1]=yl[i-1]/num/deltax;
}
for(i=0;i<part;i++) sum=sum+yl[i];
plotxy(&xl,&yl,part,mode);

}

float max(float *x, int num)
{
int i;
float max;
max=x[0];
for(i=1;i<num;i++)
{
if(x[i]>max) max=x[i];
}
return max;
}
float min(float *x, int num)
{
int i;
float min;
min=x[0];
for(i=1;i<num;i++)
{
if(x[i]<min) min=x[i];
}
return min;
}



‘捌’ 求一个VC++程序,可以产生满足高斯分布的随机数!

float CMycomDlg::Gaussrand(float miu, float sigma)
{
static double V1, V2, S;
static int phase = 0;
double X;

if ( phase == 0 )
{
do {
double U1 = (double)rand() / RAND_MAX;
double U2 = (double)rand() / RAND_MAX;

V1 = 2 * U1 - 1;
V2 = 2 * U2 - 1;
S = V1 * V1 + V2 * V2;
} while(S >= 1 || S == 0);

X = V1 * sqrt(-2 * log(S) / S);
}
else
{
X = V2 * sqrt(-2 * log(S) / S);
}

phase = 1 - phase;

return (float)(miu+sigma*X);
}

‘玖’ 用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语言怎么产生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