當前位置:首頁 » 編程語言 » 高斯隨機分布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