Created
January 20, 2016 13:48
-
-
Save ryuichimatsumoto-single/69c34c491cbe04b3d49e to your computer and use it in GitHub Desktop.
n回の試行のうち、少なくとも1回カードを引く確率を計算(作り途中)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include<stdio.h> | |
| #include<math.h> | |
| /* | |
| 少なくとも1回カードが排出される確率f(n,p)=1-(1-p)^n | |
| において、n,pの情報からf(n,p)を求める | |
| */ | |
| double ProbabilityAtReastOneTime(int n,double p) | |
| { | |
| //例外の値が入力されたときは、仕方なく0を返す | |
| if(n < 0 || p < 0 || p > 1) | |
| { | |
| printf("error 0:Please input n > 0 or 0 < p < 1 \n"); | |
| return 0.0; | |
| } | |
| //それ以外の場合は以下を出力 | |
| return 1 - pow((1 - p),n); | |
| } | |
| /* | |
| 少なくとも1回カードが排出される確率f(n,p)=1-(1-p)^n | |
| において、pを固定しf(n,p) > thresholdとなる | |
| 確率nを求める | |
| */ | |
| int ProbabilityAtReastOneTimeSampleSize(double p,double threshold) | |
| { | |
| int n = 0;//ループ用変数 | |
| double P = 0;//最終的に出力される確率 | |
| //例外の値が入力されたときは、仕方なく0を返す | |
| if(p < 0 || p > 1) | |
| { | |
| printf("error 0:Please input n > 0 or 0 < p < 1 \n"); | |
| return 0; | |
| } | |
| //それ以外の場合は以下を出力 | |
| while(P < threshold) | |
| { | |
| n++; | |
| P = 1 - pow((1 - p),n); | |
| } | |
| //printf("%d,%.10f\n",n,P); | |
| return n; | |
| } | |
| /* | |
| 少なくとも1回カードが排出される確率f(n,p)=1-(1-p)^n | |
| において、nを固定しf(n,p) > thresholdとなる | |
| 確率pを求めるプログラム | |
| deltaは:Δpで、例えばΔp=0.01ならば,p=0.01,0.02,.....と0.01刻み | |
| pの候補をループしていく。 | |
| */ | |
| double ProbabilityAtReastOneTimeCalclate(int n,double threshold,double delta) | |
| { | |
| double p = 0.0; | |
| double P = 0.0;//最終的に出力される確率 | |
| //例外の値が入力されたときは、仕方なく0を返す | |
| if(n < 0) | |
| { | |
| printf("error 0:Please input n > 0\n"); | |
| return 0; | |
| } | |
| //それ以外の場合は以下を出力 | |
| for(p=0.0;p<=1.0;p=p+delta) | |
| { | |
| P = 1 - pow((1 - p),n); | |
| if( P > threshold) break; | |
| } | |
| //printf("%d,%.10f\n",n,P); | |
| return p; | |
| } | |
| int main() | |
| { | |
| //printf("F(100,0.01)=%.15f\n",ProbabilityAtReastOneTime(100,0.01)); | |
| printf("関数2:%d\n",ProbabilityAtReastOneTimeSampleSize(0.01,0.99)); | |
| //ProbabilityAtReastOneTimeCalclate(int n,double threshold,double delta) | |
| printf("関数3:%.3f\n",ProbabilityAtReastOneTimeCalclate(100,0.99,0.001)); | |
| return 0;//ここで終了 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment