SHARE
TWEET

Untitled

a guest Oct 12th, 2017 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. int main(void)
  5. {
  6.     int counter, diceroll;
  7.     int rone=0, rtwo=0, rthree=0, rfour=0, rfive=0, rsix=0;
  8.     double pone, ptwo, pthree, pfour, pfive, psix, mean;
  9.  
  10.     counter =1;
  11.     srand((unsigned) time(NULL));
  12.     while (counter <= 1000)
  13.     {
  14.         diceroll=rand()%6+1;
  15.         if (diceroll==1)
  16.         {
  17.             rone++;
  18.         }
  19.         else if (diceroll==2)
  20.         {
  21.             rtwo++;
  22.         }
  23.         else if (diceroll==3)
  24.         {
  25.             rthree++;
  26.         }
  27.         else if (diceroll==4)
  28.         {
  29.             rfour++;
  30.         }
  31.         else if (diceroll==5)
  32.         {
  33.             rfive++;
  34.         }
  35.         else if (diceroll==6)
  36.         {
  37.             rsix++;
  38.         }
  39.         counter++;
  40.     }
  41.  
  42.     printf("Number of ones: %d\n",rone);
  43.     pone=(double)rone/1000;
  44.     printf("Probability of rolling one = %lf\n\n",pone);
  45.  
  46.     printf("Number of twos: %d\n",rtwo);
  47.     ptwo=(double)rtwo/1000;
  48.     printf("Probability of rolling two = %lf\n\n",ptwo);
  49.  
  50.     printf("Number of threes: %d\n",rthree);
  51.     pthree=(double)rthree/1000;
  52.     printf("Probability of rolling three = %lf\n\n",pthree);
  53.  
  54.     printf("Number of fours: %d\n",rfour);
  55.     pfour=(double)rfour/1000;
  56.     printf("Probability of rolling four = %lf\n\n",pfour);
  57.  
  58.     printf("Number of fives: %d\n",rfive);
  59.     pfive=(double)rfive/1000;
  60.     printf("Probability of rolling five = %lf\n\n",pfive);
  61.  
  62.     printf("Number of sixes: %d\n",rsix);
  63.     psix=(double)rsix/1000;
  64.     printf("Probability of rolling six = %lf\n\n",psix);
  65.  
  66.     mean =1*pone+2*ptwo+3*pthree+4*pfour+5*pfive+6*psix;
  67.     printf("Mean of Probability Distribution = %lf\n",mean);
  68.  
  69.     int counter1=1,counter2=1,counter3=1,counter4=1,counter5=1,counter6=1;
  70.     double var=0,var1=0,var2=0,var3=0,var4=0,var5=0,var6=0,standarddeviation=0;
  71.  
  72.     while (counter1 <= rone)
  73.     {
  74.         var1 = var1 + pow(1-mean,2);
  75.         counter1++;
  76.     }
  77.     while (counter2 <= rtwo)
  78.     {
  79.         var2 = var2 + pow(2-mean,2);
  80.         counter2++;
  81.     }
  82.     while (counter3 <= rthree)
  83.     {
  84.         var3 = var3 + pow(3-mean,2);
  85.         counter3++;
  86.     }
  87.     while (counter4 <= rfour)
  88.     {
  89.         var4 = var4 + pow(4-mean,2);
  90.         counter4++;
  91.     }
  92.     while (counter5 <= rfive)
  93.     {
  94.         var5 = var5 + pow(5-mean,2);
  95.         counter5++;
  96.     }
  97.     while (counter6 <= rsix)
  98.     {
  99.         var6 = var6 + pow(6-mean,2);
  100.         counter6++;
  101.     }
  102.  
  103.        var = (var1 + var2 + var3 + var4 + var5 + var6)/1000;
  104.        printf("Variance = %lf\n",var);
  105.        standarddeviation = sqrt(var);
  106.        printf("Standard Deviation = %lf\n",standarddeviation);
  107.  
  108. return 0;
  109. }
RAW Paste Data
Top