Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- int main(void)
- {
- int counter, diceroll;
- int rone=0, rtwo=0, rthree=0, rfour=0, rfive=0, rsix=0;
- double pone, ptwo, pthree, pfour, pfive, psix, mean;
- counter =1;
- srand((unsigned) time(NULL));
- while (counter <= 1000)
- {
- diceroll=rand()%6+1;
- if (diceroll==1)
- {
- rone++;
- }
- else if (diceroll==2)
- {
- rtwo++;
- }
- else if (diceroll==3)
- {
- rthree++;
- }
- else if (diceroll==4)
- {
- rfour++;
- }
- else if (diceroll==5)
- {
- rfive++;
- }
- else if (diceroll==6)
- {
- rsix++;
- }
- counter++;
- }
- printf("Number of ones: %d\n",rone);
- pone=(double)rone/1000;
- printf("Probability of rolling one = %lf\n\n",pone);
- printf("Number of twos: %d\n",rtwo);
- ptwo=(double)rtwo/1000;
- printf("Probability of rolling two = %lf\n\n",ptwo);
- printf("Number of threes: %d\n",rthree);
- pthree=(double)rthree/1000;
- printf("Probability of rolling three = %lf\n\n",pthree);
- printf("Number of fours: %d\n",rfour);
- pfour=(double)rfour/1000;
- printf("Probability of rolling four = %lf\n\n",pfour);
- printf("Number of fives: %d\n",rfive);
- pfive=(double)rfive/1000;
- printf("Probability of rolling five = %lf\n\n",pfive);
- printf("Number of sixes: %d\n",rsix);
- psix=(double)rsix/1000;
- printf("Probability of rolling six = %lf\n\n",psix);
- mean =1*pone+2*ptwo+3*pthree+4*pfour+5*pfive+6*psix;
- printf("Mean of Probability Distribution = %lf\n",mean);
- int counter1=1,counter2=1,counter3=1,counter4=1,counter5=1,counter6=1;
- double var=0,var1=0,var2=0,var3=0,var4=0,var5=0,var6=0,standarddeviation=0;
- while (counter1 <= rone)
- {
- var1 = var1 + pow(1-mean,2);
- counter1++;
- }
- while (counter2 <= rtwo)
- {
- var2 = var2 + pow(2-mean,2);
- counter2++;
- }
- while (counter3 <= rthree)
- {
- var3 = var3 + pow(3-mean,2);
- counter3++;
- }
- while (counter4 <= rfour)
- {
- var4 = var4 + pow(4-mean,2);
- counter4++;
- }
- while (counter5 <= rfive)
- {
- var5 = var5 + pow(5-mean,2);
- counter5++;
- }
- while (counter6 <= rsix)
- {
- var6 = var6 + pow(6-mean,2);
- counter6++;
- }
- var = (var1 + var2 + var3 + var4 + var5 + var6)/1000;
- printf("Variance = %lf\n",var);
- standarddeviation = sqrt(var);
- printf("Standard Deviation = %lf\n",standarddeviation);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement