Check out the Pastebin Gadgets Shop. We have thousands of fun, geeky & affordable gadgets on sale :-)Want more features on Pastebin? Sign Up, it's FREE!
tweet

# Untitled

By: a guest on Oct 11th, 2012  |  syntax: C  |  size: 1.19 KB  |  views: 35  |  expires: Never
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
1.
2. #include <stdlib.h>
3. #include <time.h>
4. #include <stdio.h>
5. #include <math.h>
6.
7. int f1(){
8.     return rand()%4; // 1/4 chance
9. }
10.
11. int f2(){
12.     return rand()%50; // 1/50 chance
13. }
14.
15.
16. int f3(){
17.     return rand()%100; // 1/100 chance
18. }
19.
20. int main(){
21.
22.     double total[3] = {0.0},maxucb,newucb;
23.     int tried[3] = {0};
24.     int triedf = 0;
25.     int i,j,n,maxind;
26.         srand(time(NULL));
27.     int  (*f[3])() = {f1,f2,f3};
28.
29.     for(j=0;j<3;j++){
30.       if(f[j]() == 0) total[j]+=1;
31.       tried[j]++;
32.       triedf++;
33.     }
34.
35.
36.     for(i=0;i<10000;i++){
37.         maxucb = total[0]/triedf + sqrt((2*(log(triedf)))/tried[0]);
38.         maxind = 0;
39.         for(n=1;n<3;n++){
40.
41.             newucb = total[n]/tried[n] + sqrt((2*(log(triedf)))/tried[n]);
42.             if(newucb > maxucb){
43.                 maxucb = newucb;
44.                 maxind = n;
45.             }
46.         }
47.         if(f[maxind]() == 0) total[maxind]+=1;;
48.         triedf++;
49.         tried[maxind]++;
50.     }
51.
52.     for(n=0;n<3;n++){
53.       printf("win percentage: %f tries: %d\n",total[n]/tried[n],tried[n]);
54.     }
55.
56.     system("pause");
57.
58.
59.     return 0;
60.
61. }
clone this paste RAW Paste Data
Top