fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int creature(int n){
  5. int gold=0;
  6. int creature=n;
  7. float r;
  8. while(creature>0){
  9. r=(float)rand()/RAND_MAX;
  10. creature-=1;
  11. if (r <= 0.9){
  12. gold++;
  13. }else{
  14. creature += 2;
  15. }
  16. }
  17. return gold;
  18. }
  19. int main(){
  20. srand(time(NULL));
  21. int sum = 0;
  22.  
  23. for (int i = 0; i < 100; i++) {
  24. int g = creature(100);
  25. printf("%d\n", g);
  26. sum += g;
  27. }
  28. printf("sum = %d\n", sum);
  29. printf("ave=%f\n", (float)sum / 100);
  30.  
  31. return 0;
  32. }
Success #stdin #stdout 0s 5320KB
stdin
Standard input is empty
stdout
120
108
112
110
112
117
107
107
110
108
108
113
116
110
111
114
109
115
110
112
112
118
112
111
111
106
107
112
104
117
115
112
116
119
116
120
119
106
113
115
114
121
115
116
109
126
118
117
108
118
121
106
109
115
110
110
111
112
111
110
111
111
110
113
114
110
112
114
118
110
112
116
112
115
109
109
110
112
112
108
119
117
105
108
118
110
110
111
110
112
108
111
106
105
108
115
111
108
113
109
sum = 11219
ave=112.190002