public
Last active

My roommate's friend knew he needed help with stats/probability. He had no idea he also needed help with his code.

  • Download Gist
Bogorand.c
C
1 2 3 4 5
int random;
do
{
random=rand();
} while(random < 1 || random > 100);
JKrtest.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
//////////////////////////////////////////////////////////////////////
// Title: *removed*
// Programmer: *removed*
// Class: *removed*
// Date: *removed*
/////////////////////////////////////////////////////////////////////////
 
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
 
int sucess(int percentage); // function declaration
 
int main(void)
{
 
int i_count = 0;
int i = 0;
int iteration = 0;
int n = 0;
int a = 0;
int b = 0;
int c = 0;
int d = 0;
int e = 0;
float reliability = 0 ;
float failure = 0 ;
 
int A = 80;
int B = 75;
int C = 90;
int D = 90;
int E = 70;
 
FILE *spOut;
int file_close;
 
spOut=fopen("JKtext.txt","w");
 
srand(time(NULL));
printf("\n\nHow many times do you want to run? ");
scanf("%d", &n);
//getchar();
 
iteration = n;
printf("\nRunning iteration %d times", iteration);
for(i=0;i<iteration;i++)
{
a=success(A);
b=success(B);
c=success(C);
d=success(D);
e=success(E);
 
if((a || b) && d)
i_count++;
else if(c && e)
i_count++;
}
 
 
reliability = ((float) i_count/iteration)*100;
failure = 100 - reliability;
 
printf( "\n====================================================\n");
printf( "==== Circuit Reliability Rate is %.2f%%=============\n", reliability);
 
printf( "====================================================\n\n");
 
printf( "*****************************************************\n");
printf( "***** Circuit Failure Rate is %.2f%%******************\n", failure);
printf( "*****************************************************\n");
fprintf(spOut, "%.2f\n", reliability);
fprintf(spOut, "%.2f\n", failure);
 
file_close = fclose(spOut);
 
printf("\nA text file has been produced. ");
 
 
 
return 0;
}
 
//FUNCTIONS
int success(int percentage)
{
int random;
do
{
random=rand();
} while(random < 1 || random > 100);
 
if(random > percentage)
{
return 0;
}
else
{
return 1;
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.