Compare commits
13 Commits
Author | SHA1 | Date | |
---|---|---|---|
537bc64413 | |||
bea82bc7f5 | |||
4d89477bce | |||
5c1c5654d1 | |||
fd31a58e26 | |||
072029db8a | |||
53d372675d | |||
231437872a | |||
0e63f6af23 | |||
165213a1ad | |||
f116a4727c | |||
23e0889167 | |||
ed5f340e68 |
8
Makefile
8
Makefile
@ -9,17 +9,17 @@ MKDIR=mkdir -p
|
||||
RM=rm -rf
|
||||
|
||||
SRC_FILES := $(wildcard src/*.c)
|
||||
OBJ_FILES := $(addprefix src/, $(notdir $(SRC_FILES:.c=.o)))
|
||||
OBJ_FILES := $(addprefix obj/, $(notdir $(SRC_FILES:.c=.o)))
|
||||
|
||||
all: $(OBJ_FILES)
|
||||
$(MKDIR) bin
|
||||
$(CXX) $(CXXFLAGS) $(LIBS) -o bin/cointoss src/*.o
|
||||
$(CXX) $(CXXFLAGS) $(LIBS) -o bin/cointoss obj/*.o
|
||||
|
||||
src/%.o: src/%.c
|
||||
obj/%.o: src/%.c
|
||||
$(CXX) $(CXXFLAGS) $(INCLUDES) -c -o $@ $<
|
||||
|
||||
clean:
|
||||
$(RM) src/*.o
|
||||
$(RM) obj/*.o
|
||||
|
||||
cleanall: clean
|
||||
$(RM) bin
|
||||
|
@ -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:
|
||||
|
1
obj/readme.txt
Normal file
1
obj/readme.txt
Normal file
@ -0,0 +1 @@
|
||||
This is where the object output files go. This readme file is a simple hack so that git registers it (git doesn't register empty directories).
|
64
src/main.c
64
src/main.c
@ -16,52 +16,85 @@
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <time.h>
|
||||
|
||||
char version[7] = "v0.1";
|
||||
// Global version string
|
||||
char version[7] = "v0.2";
|
||||
|
||||
/* Function prototypes start */
|
||||
void printCopyright();
|
||||
void printAllCopyright();
|
||||
void printWarranty();
|
||||
/* Function prototypes end */
|
||||
|
||||
// Main function:
|
||||
int main(int argc, char **argv) {
|
||||
if(argc < 2 || argc > 3) {
|
||||
printf("Usage: %s [trials]\n", argv[0]);
|
||||
// If inappropriate number of arguments
|
||||
if(argc < 2 || argc > 3) { // If inappropriate number of arguments (number required = 2)
|
||||
printf("Inappropriate amount of arguments.\n");
|
||||
printf("Usage: %s [num_trials]\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
if(strcmp(argv[1], "-c") == 0) {
|
||||
if(!strcmp(argv[1], "-c")) { // If argv[1] (the second argument) is equal to "-c" ...
|
||||
printAllCopyright();
|
||||
return 0;
|
||||
} else if(strcmp(argv[1], "-w") == 0) {
|
||||
} else if(!strcmp(argv[1], "-w")) { // If argv[1] is equal to "-w"
|
||||
printWarranty();
|
||||
return 0;
|
||||
} else if(!strcmp(argv[1], "-h") || !strcmp(argv[1], "--help")) { // If argv[1] is equal to "-h" or "--help"
|
||||
printf("Usage: %s [num_trials]\n\
|
||||
-c -- Print copyright info\n\
|
||||
-h | --help -- Print this help information\n\
|
||||
-w -- Print warranty information\n", argv[0]);
|
||||
return 0;
|
||||
} 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')) { // 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;
|
||||
}
|
||||
i++;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
|
||||
printf("Working...\n");
|
||||
for(int i = 0; i < trials; i++) {
|
||||
if(i % 100000000 == 0) printf("Working...\n");
|
||||
int r = rand() % 100 + 1;
|
||||
long long int oldprogress = 0;
|
||||
|
||||
printf("Calculating random numbers...\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;
|
||||
}
|
||||
|
||||
/* Defined functions start */
|
||||
void printCopyright() {
|
||||
printf("CoinToss %s Copyright (C) 2014 Nicolás A. Ortega\n\
|
||||
This program comes with ABSOLUTELY NO WARRANTY; for details use `-w'.\n\
|
||||
@ -94,3 +127,4 @@ PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE PROGRAM\n\
|
||||
IS WITH YOU. SHOULD THE PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF\n\
|
||||
ALL NECESSARY SERVICING, REPAIR OR CORRECTION.\n");
|
||||
}
|
||||
/* Defined functions end */
|
||||
|
Reference in New Issue
Block a user