fork download
  1. #include <stdio.h>
  2. #include <time.h>
  3. int main(void) {
  4. int target[5],hit,count=0;
  5. srand(time(NULL));
  6. int sample = 10000000;
  7. for (int i=1;i<=sample;i++){
  8. target[0]=1;
  9. target[1]=2;
  10. target[2]=2;
  11. target[3]=4;
  12. target[4]=30;
  13. for (int j=1;j<=9;j++){
  14. hit = (rand()%5);
  15. if(target[hit]==0)j--;
  16. else target[hit]--;
  17. }
  18. if (target[4]==30) count++;
  19. }
  20. printf("percentage: %.6f",((float)count)/sample);
  21. return 0;
  22. }
  23.  
  24.  
Success #stdin #stdout 1.52s 9424KB
stdin
Standard input is empty
stdout
percentage: 0.033294