Probabilities

//don't forget to srand(time(NULL)) at the beginning of main() //this snippet requires a programming language that allows function overloading, check if yours do so //be sure to use the correct one between float and double ! bool proba(int x){ //pre-condition : 0<=x<=100 if(x==0){return 0;} if(x==100){return 1;} int res = rand()%101; return (res<=x); } bool proba(float x){ //pre-condition : 0<=x<=1 if(x==0.0){return 0;} if(x==1.0){return 1;} int res = rand()%101; float r = (float)((float)(res)/100.0); return (r<=x); } bool proba(double x){ //pre-condition : 0<=x<=1 if(x==0.00){return 0;} if(x==1.00){return 1;} int res = rand()%101; double r = (double)((double)(res)/100.00); return (r<=x); } bool pProba(float x){ //pre-condition : 0.0<=x<=100.0 if(x==0.0){return 0;} if(x==100.0){return 1;} int int_part = rand()%101; int dec_part = 0; if(int_part!=100){dec_part=rand()%100;} float r = (float)(int_part) + (float)( (float)(dec_part) / 100 ); return (r<=x); } bool pProba(double x){ //pre-condition : 0.00<=x<=100.00 if(x==0.00){return 0;} if(x==100.00){return 1;} int int_part = rand()%101; int dec_part = 0; if(int_part!=100){dec_part=rand()%100;} double r = (double)(int_part) + (double)( (double)(dec_part) / 100 ); return (r<=x); }
test if an event occurred according to a probability passed in the function

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.