Fixed progress problem by using long long int
This commit is contained in:
parent
4d89477bce
commit
bea82bc7f5
@ -1,7 +1,7 @@
|
|||||||
CoinToss
|
CoinToss
|
||||||
========
|
========
|
||||||
|
|
||||||
The creation of this program has a little story. I was in math class and a classmate was playing around with the probability simulator on his TI Calculator. He was trying to do as many coin tosses as he could, until finally he reached 9999, which was the limit of tosses for the simulator. So I decided that I would create this, which allows you to do more than that, specifically 2,147,473,649 more times than that.
|
The creation of this program has a little story. I was in math class and a classmate was playing around with the probability simulator on his TI Calculator. He was trying to do as many coin tosses as he could, until finally he reached 9999, which was the limit of tosses for the simulator. So I decided that I would create this, which allows you to do more than that, specifically 9223372036854765808 more trials than that.
|
||||||
|
|
||||||
### Compiling
|
### Compiling
|
||||||
To compile make sure you have the GNU C Compiler installed (GCC) and just go to the root directory of this project and run:
|
To compile make sure you have the GNU C Compiler installed (GCC) and just go to the root directory of this project and run:
|
||||||
|
29
src/main.c
29
src/main.c
@ -34,7 +34,7 @@ void printWarranty();
|
|||||||
int main(int argc, char **argv) {
|
int main(int argc, char **argv) {
|
||||||
// If inappropriate number of arguments
|
// If inappropriate number of arguments
|
||||||
if(argc < 2 || argc > 3) { // If inappropriate number of arguments (number required = 2)
|
if(argc < 2 || argc > 3) { // If inappropriate number of arguments (number required = 2)
|
||||||
printf("Inappropriate amount of arguments.");
|
printf("Inappropriate amount of arguments.\n");
|
||||||
printf("Usage: %s [num_trials]\n", argv[0]);
|
printf("Usage: %s [num_trials]\n", argv[0]);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
@ -65,33 +65,32 @@ int main(int argc, char **argv) {
|
|||||||
|
|
||||||
printCopyright();
|
printCopyright();
|
||||||
|
|
||||||
int trials = atoi(argv[1]);
|
long long int trials = atoi(argv[1]);
|
||||||
int heads = 0;
|
long long int heads = 0;
|
||||||
int tails = 0;
|
long long int tails = 0;
|
||||||
|
|
||||||
// Setup the random number generator with the seed being the time
|
// Setup the random number generator with the seed being the time
|
||||||
srand(time(NULL));
|
srand(time(NULL));
|
||||||
|
|
||||||
//int oldprogress = 0;
|
long long int oldprogress = 0;
|
||||||
|
|
||||||
printf("Calculating random numbers...\n");
|
printf("Calculating random numbers...\n");
|
||||||
//printf("Progress: 0 %%\n");
|
printf("Progress: 0 %%\n");
|
||||||
for(int i = 0; i < trials; i++) {
|
for(long long int i = 0; i < trials; i++) {
|
||||||
/*int progress = i * 100 / trials;
|
long long int progress = (i * 100) / trials;
|
||||||
if(progress != oldprogress && progress % 10 == 0) {
|
if(progress != oldprogress && progress % 10 == 0) {
|
||||||
printf("Progress: %i %%\n", progress);
|
printf("Progress: %lli %%\n", progress);
|
||||||
oldprogress = progress;
|
oldprogress = progress;
|
||||||
}*/
|
}
|
||||||
//if(i % 100000000 == 0) printf("Working...\n");
|
|
||||||
int r = rand() % 100 + 1; // Calculate random number between 1 and 100
|
int r = rand() % 100 + 1; // Calculate random number between 1 and 100
|
||||||
if(r <= 50) heads++;
|
if(r <= 50) heads++;
|
||||||
else tails++;
|
else tails++;
|
||||||
}
|
}
|
||||||
//printf("Progress: 100 %%\n");
|
printf("Progress: 100 %%\n");
|
||||||
|
|
||||||
printf("Done.\n");
|
printf("Done.\n\n");
|
||||||
printf("Heads: %i\n", heads);
|
printf("Heads: %lli\n", heads);
|
||||||
printf("Tails: %i\n", tails);
|
printf("Tails: %lli\n", tails);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user