###### tags: `code` # c14086028-homework-8-1 ###### tags `use'c' to analysis the max probability of something` ```cpp= #include<stdio.h> #include<stdlib.h> #include<time.h> /*number of times of test*/ #define N 10000 /*number2 of times of test*/ #define n 1000 /*width of interval*/ #define I 5 int main() { int R[N] = { 0 }; srand(time(NULL)); /*compute the final point*/ for (int a = 0; a < N; a++) { for (int b = 0; b < n; b++) { int c; c = rand() % 2; if (c == 1) { R[a] = R[a] + 1; } else { R[a] = R[a] - 1; } } } /*selection sort*/ for (int i = 0; i < N; i++) { for (int j = i; j < N; j++) { if (R[j] < R[i]) { int r1 = 0; r1 = R[i]; R[i] = R[j]; R[j] = r1; } } } printf("-------------\n"); /*compute and record and compare th number of times the final point pass the interval*/ int r[2*n/5] = { 0 }; int x1 = 0,y1=0; int k = 0; for (int i = R[0]; i <= R[N - 1]; i=i + I) { for (int a = 0; a < N; a++) { if (i+I==R[N-1]) { if (i <= R[a] && R[a] <= i + I) { r[k] = r[k] + 1; } } if (i <= R[a] && R[a] < i+I) { r[k]=r[k]+1; } } printf("the nnumber of times of final point in [%d,%d) is %d\n", i, i + I, r[k]); printf("-------------\n"); if (r[k] > y1) { y1 = r[k]; x1 = i; } k++; } printf("-------------\n"); printf("%d\n", k); printf("the interval which have most times of final point is [%d,%d),it pass %d times", x1, x1 + I,y1); system("pause"); return 0; } ``` ![](https://i.imgur.com/DvdzAah.png =200%x)圖一 ![](https://i.imgur.com/2IwU3UM.png =200%x)圖二 ***圖一*** :為採樣30次最大出現次數點後,以該區間的中點為x軸,次數$/100$ 為 y軸. ***圖二*** :為執行一次把所有可能存在點的區間印出來後的結果 由**圖一**可以發現最大出現次數點集中在$[-16,16]$區間的子區間之中(分別為$[-16,-8]、[2,4]、[12,15]$,但這可能是因為採樣點過少的結果。 由**圖二**知道出現次數較多的點大概會在$[-25,25]$之間而可能的最大結果大概率會出現在$[-16,16]$ 由微積分上課而知,他可能會很像Gaussian'sfunction,經由調整係數可以讓當區間為5時,猜測會越來越趨近下圖 ![](https://i.imgur.com/pTMvGQX.png =100%x) 此為圖為$(\frac{1.8}{\pi})e^{\frac{-x^2}{2}}$ 因此,我們可以大致推敲如果把y軸值設為出現次數$/10000$,區間調小為2,猜測會越來越趨近下圖 ![](https://i.imgur.com/oH4EQqr.png =100%x) 此為圖為$(\frac{1}{\sqrt{4.5\pi}})e^{\frac{-x^2}{2}}$