Compare commits
4 Commits
fd31a58e26
...
v0.2
Author | SHA1 | Date | |
---|---|---|---|
537bc64413 | |||
bea82bc7f5 | |||
4d89477bce | |||
5c1c5654d1 |
@ -1,7 +1,7 @@
|
||||
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 9,223,372,036,854,765,808 more trials than that.
|
||||
|
||||
### 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:
|
||||
|
28
src/main.c
28
src/main.c
@ -34,7 +34,7 @@ void printWarranty();
|
||||
int main(int argc, char **argv) {
|
||||
// If inappropriate number of arguments
|
||||
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]);
|
||||
return 1;
|
||||
}
|
||||
@ -54,7 +54,7 @@ int main(int argc, char **argv) {
|
||||
} else {
|
||||
int i = 0;
|
||||
while(argv[1][i] != '\0') { // While argv[1][i] is not equal to NULL (can't use NULL for pointers)
|
||||
if(isdigit(argv[1][i] - '0') != 0) { // if argv[1][i] is not a number between 0 and 9
|
||||
if(isdigit(argv[1][i] - '0')) { // if argv[1][i] is not a number between 0 and 9
|
||||
printf("You did not specify a number.\n");
|
||||
printf("Usage: %s [num_trials]\n", argv[0]);
|
||||
return 1;
|
||||
@ -65,24 +65,32 @@ int main(int argc, char **argv) {
|
||||
|
||||
printCopyright();
|
||||
|
||||
int trials = atoi(argv[1]);
|
||||
int heads = 0;
|
||||
int tails = 0;
|
||||
long long int trials = atoi(argv[1]);
|
||||
long long int heads = 0;
|
||||
long long int tails = 0;
|
||||
|
||||
// Setup the random number generator with the seed being the time
|
||||
srand(time(NULL));
|
||||
|
||||
long long int oldprogress = 0;
|
||||
|
||||
printf("Calculating random numbers...\n");
|
||||
for(int i = 0; i < trials; i++) {
|
||||
if(i % 100000000 == 0) printf("Working...\n");
|
||||
printf("Progress: 0 %%\n");
|
||||
for(long long int i = 0; i < trials; i++) {
|
||||
long long int progress = (i * 100) / trials;
|
||||
if(progress != oldprogress && progress % 10 == 0) {
|
||||
printf("Progress: %lli %%\n", progress);
|
||||
oldprogress = progress;
|
||||
}
|
||||
int r = rand() % 100 + 1; // Calculate random number between 1 and 100
|
||||
if(r <= 50) heads++;
|
||||
else tails++;
|
||||
}
|
||||
printf("Progress: 100 %%\n");
|
||||
|
||||
printf("Done.\n");
|
||||
printf("Heads: %i\n", heads);
|
||||
printf("Tails: %i\n", tails);
|
||||
printf("Done.\n\n");
|
||||
printf("Heads: %lli\n", heads);
|
||||
printf("Tails: %lli\n", tails);
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user