33 lines
649 B
C
33 lines
649 B
C
|
#include <stdio.h>
|
||
|
#include <stdlib.h>
|
||
|
|
||
|
#define TOTAL_ATTEMPTS 15000000
|
||
|
|
||
|
int get_random_bit()
|
||
|
{
|
||
|
int r = rand() % 256;
|
||
|
return r;
|
||
|
}
|
||
|
|
||
|
int main()
|
||
|
{
|
||
|
int successful_attempts = 0;
|
||
|
|
||
|
for (int i = 0; i < TOTAL_ATTEMPTS; i++)
|
||
|
{
|
||
|
int a = get_random_bit();
|
||
|
int b = get_random_bit();
|
||
|
int c = get_random_bit();
|
||
|
|
||
|
if (a + b + c > 300)
|
||
|
{
|
||
|
successful_attempts++;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
printf("Iterations: %d\n", TOTAL_ATTEMPTS);
|
||
|
printf("Valid sums: %d\n", successful_attempts);
|
||
|
printf("Probability: %lf\n", (double) ((double) successful_attempts / (double) TOTAL_ATTEMPTS));
|
||
|
|
||
|
return 0;
|
||
|
}
|