用c語言 產生服從均勻分布, 瑞利分布,萊斯分布,高斯分布的隨機數
一,各個分布對應的基本含義:
- 1. 均勻分布或稱規則分布,顧名思義,均勻的,不偏差的。植物種群的個體是等距分布,或個體之間保持一定的均勻的間距。
- 2. 高斯分布, 即正態分布(Normal distribution),也稱“常態分布”,又名高斯分布(Gaussian distribution),最早由A.棣莫弗在求二項分布的漸近公式中得到。C.F.高斯在研究測量誤差時從另一個角度導出了它。P.S.拉普拉斯和高斯研究了它的性質。[1] 是一個在數學、物理及工程等領域都非常重要的概率分布,在統計學的許多方面有着重大的影響力。正態曲線呈鍾型,兩頭低,中間高,左右對稱因其曲線呈鍾形,因此人們又經常稱之為鍾形曲線。若隨機變量X服從一個數學期望為μ、方差為σ^2的正態分布,記為N(μ,σ^2)。其概率密度函數為正態分布的期望值μ決定了其位置,其標准差σ決定了分布的幅度。當μ = 0,σ = 1時的正態分布是標准正態分布。
- 3. 瑞利分布(Rayleigh Distribution):當一個隨機二維向量的兩個分量呈獨立的、有着相同的方差的正態分布時,這個向量的模呈瑞利分布.
- 4. 萊斯分布(Rice distribution或Rician distribution)是一種連續概率分布,以美國科學家斯蒂芬·萊斯(en:Stephen O. Rice)的名字命名。 正弦波加窄帶高斯過程的包絡概率密度函數分布稱為萊斯(Rice)密度函數,也稱廣義瑞利分布。
二, 各個分布對應的隨機變量產生的算法,
1 # include "stdio.h" 2 # include "math.h" 3 # include "stdlib.h" 4 # include "math.h" 5 # include "dos.h" 6 # define MAX_N 3000 /*這個值為N可以定義的最大長度*/ 7 # define N 100 /*產生隨機序列的點數,注意不要大於MAX_N*/ 8 9 /*1.產生均勻分布的隨機變量*/ 10 void randa(float *x,int num); 11 12 /*2.產生瑞利分布的隨機變量*/ 13 void randr(float *x,int num); 14 15 /*3.產生標准高斯分布的隨機變量*/ 16 void randn(float *x,int num); 17 18 /*4.產生萊斯分布的隨機變量*/ 19 void randl(float *x, float a, float b, int num); 20 21 void fshow(char *name,float *x,int num); 22 23 /***************************************/ 24 int main() 25 { 26 27 float x[N]; 28 int i; 29 30 // randa(&x,N); //均勻分布 31 // randr(&x,N); //瑞利分布 32 // randl(&x,10,10,N); //萊斯分布 33 randn(&x,N); //高斯分布 34 35 /*此時x[N]表示要生成N個服從xx分布的的數組*/ 36 37 38 fshow("x",&x,N); /*顯示該序列*/ 39 40 getch();
return 0; 41 42 } 43 /***************函數定義************************/ 44 45 /*產生服從均勻分布的隨機變量*/ 46 void randa(float *x,int num) 47 { 48 int i; 49 struct time stime; 50 unsigned seed; 51 gettime(&stime); 52 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 53 srand(seed); 54 for(i=0;i<num;i++) 55 { 56 x[i]=rand(); 57 x[i]=x[i]/32768; 58 } 59 } 60 /*產生服從瑞利分布的隨機變量*/ 61 void randr(float *x,int num) 62 { 63 float x1[MAX_N]; 64 int i; 65 struct time stime; 66 unsigned seed; 67 gettime(&stime); 68 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 69 srand(seed); 70 for(i=0;i<num;i++) 71 { 72 x1[i]=rand(); 73 x[i]=x1[i]/32768; 74 x[i]=sqrt(-2*log(x[i])); 75 } 76 77 } 78 /*產生服從標准高斯分布的隨機變量*/ 79 void randn(float *x,int num) 80 { 81 float x1[MAX_N],x2[MAX_N]; 82 int i; 83 struct time stime; 84 unsigned seed; 85 gettime(&stime); 86 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 87 srand(seed); 88 for(i=0;i<num;i++) 89 { 90 x1[i]=rand(); 91 x2[i]=rand(); 92 x1[i]=x1[i]/32768; 93 x2[i]=x2[i]/32768; 94 x[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI); 95 } 96 97 } 98 /*產生服從萊斯分布的隨機變量*/ 99 void randl(float *x, float a, float b, int num) 100 { 101 float x1[MAX_N],x2[MAX_N]; 102 float temp[MAX_N]; 103 int i; 104 struct time stime; 105 unsigned seed; 106 gettime(&stime); 107 seed=stime.ti_hund*stime.ti_min*stime.ti_hour; 108 srand(seed); 109 for(i=0;i<num;i++) 110 { 111 x1[i]=rand(); 112 x2[i]=rand(); 113 x1[i]=x1[i]/32768; 114 x2[i]=x2[i]/32768; 115 temp[i]=sqrt(-2*log(x1[i]))*cos(x2[i]*M_PI); 116 x2[i]=sqrt(-2*log(x1[i]))*sin(x2[i]*M_PI); 117 x1[i]=temp[i]; 118 x[i]=sqrt((a+x1[i])*(a+x1[i])+(b+x2[i])*(b+x2[i])); 119 } 120 121 } 122 123 void fshow(char *name,float *x,int num) 124 { 125 int i,sign,L; 126 float temp; 127 printf("\n"); 128 printf(name); 129 printf(" = "); 130 L=6; 131 /*按照每行6個數據的格式顯示*/ 132 for(i=0;i<num;i++) 133 { 134 temp=i/L; 135 sign=temp; 136 if((i-sign*L)==0) printf("\n"); 137 if(x[i]>0) printf(" %f ",x[i]); 138 else printf("%f ",x[i]); 139 } 140 }
其他分布的詳細介紹, 請戳這里:http://www.math.uah.edu/stat/special/index.html
國外知名網站給出的各種分布的曲線圖(后台程序驅動):

---OVER---
附錄: Cauchy 分布 隨機數生成代碼:
1 import math 2 import random 3 4 def cauchy(location, scale): 5 6 # Start with a uniform random sample from the open interval (0, 1). 7 # But random() returns a sample from the half-open interval [0, 1). 8 # In the unlikely event that random() returns 0, try again. 9 10 p = 0.0 11 while p == 0.0: 12 p = random.random() 13 14 return location + scale*math.tan(math.pi*(p - 0.5))