patterncMinor
Powerball Lottery Simulator in C
Viewed 0 times
powerballsimulatorlottery
Problem
I made a Powerball lottery simulator in C. I chose C because it is fast.
Here's how it plays:
My main problem is that when I run this to not stop until all 6 balls match, I am waiting for days (real life days) without a match. I comment out the
What else can I do to improve efficiency?
Here's the project on GitHub if anyone wants to try it out.
main.c:
```
/* *** This program simulates a person playing the powerball
*** everyday and stops once a certain number of balls
*** match. Ther user decides how many balls should match
*** before stopping. */
/* DEFINTIONS:
redball and powerball refer to the same thing
*/
#include
#include
/ for rand(), srand() /
#include
#include
/ for time(), to seed srand() /
/ functions to get user input for program options /
#include "options.h"
/ functions to do the calculation /
#include "calc.h"
/ functions for output /
#include "output.h"
int main(void)
{
/ customer ticket, powerball drawing /
int ticket[6] = {0};
int draw[6] = {0};
/ how many in-program days have we been playing? /
int days = 0;
int years = 0;
/ how much did the player win? /
int winnings = 0;
/ Intializes random number generator /
Here's how it plays:
- The user plays one ticket every day.
- The user can choose to play the same ticket every day or a different ticket every day.
- The numbers on each ticket are a "quick pick" -- that is, randomly generated.
- The program also generates a random winning drawing every day.
- The program stops when a user-decided number of balls on that day's ticket match with the same number of balls in the drawing.
- The user decides whether the powerballs (the 6th balls) must match before the program stops.
- The program ends with telling the user how many tickets they bought, how many years and days they played, and how much they won.
My main problem is that when I run this to not stop until all 6 balls match, I am waiting for days (real life days) without a match. I comment out the
printf() lines in main() that show the numbers for better efficiency, but I still have yet to get a match on all six numbers.What else can I do to improve efficiency?
Here's the project on GitHub if anyone wants to try it out.
main.c:
```
/* *** This program simulates a person playing the powerball
*** everyday and stops once a certain number of balls
*** match. Ther user decides how many balls should match
*** before stopping. */
/* DEFINTIONS:
redball and powerball refer to the same thing
*/
#include
#include
/ for rand(), srand() /
#include
#include
/ for time(), to seed srand() /
/ functions to get user input for program options /
#include "options.h"
/ functions to do the calculation /
#include "calc.h"
/ functions for output /
#include "output.h"
int main(void)
{
/ customer ticket, powerball drawing /
int ticket[6] = {0};
int draw[6] = {0};
/ how many in-program days have we been playing? /
int days = 0;
int years = 0;
/ how much did the player win? /
int winnings = 0;
/ Intializes random number generator /
Solution
I choose C because it is fast.
You should think about your priorities for the code. The first priority should be correctness, and C is a terrible choice for that goal, since a bug in one part of the program (especially buffer overflows) can influence other totally unrelated parts in unpredictable ways. If you prefer to have working code over fast code, you should look for a language that checks array boundaries and invalid pointers.
Choosing a "fast" programming language is useless when you don't tell the compiler that you want fast code. By default, C compilers produce slow code. (I'll come back to this topic later, in the
Your code has lots of typos. Run it through a spell checker to catch the obvious ones at least.
Why do you say "redball and powerball refer to the same thing", when the word "redball" doesn't appear in the code that follows? That's unnecessary. Remove that comment.
Many of your comments are redundant. Remove all comments that only repeat what the code already says.
The code
The code
Fix the indentation of the code. The
Don't lie in comments. "print a space" is not what
Better yet: remove all the comments that describe code, only keep those that explain the variables further. Your code is structured so well, and the variables and functions have so good names that you don't need any comments at all.
In
In
From a usability point of view, it is confusing for the user to type
The line
In the
You should think about your priorities for the code. The first priority should be correctness, and C is a terrible choice for that goal, since a bug in one part of the program (especially buffer overflows) can influence other totally unrelated parts in unpredictable ways. If you prefer to have working code over fast code, you should look for a language that checks array boundaries and invalid pointers.
Choosing a "fast" programming language is useless when you don't tell the compiler that you want fast code. By default, C compilers produce slow code. (I'll come back to this topic later, in the
Makefile section.)Your code has lots of typos. Run it through a spell checker to catch the obvious ones at least.
Why do you say "redball and powerball refer to the same thing", when the word "redball" doesn't appear in the code that follows? That's unnecessary. Remove that comment.
Many of your comments are redundant. Remove all comments that only repeat what the code already says.
The code
variable == false is usually written as !variable.The code
variable == true is usually written as variable.Fix the indentation of the code. The
then branch of each if statement must start at the same column as the corresponding else branch. There are programs that do the indentation for you, such as GNU indent. Use these tools.Don't lie in comments. "print a space" is not what
printf("\n") does, since a space is commonly thought of as being a horizontal space, not a vertical space.Better yet: remove all the comments that describe code, only keep those that explain the variables further. Your code is structured so well, and the variables and functions have so good names that you don't need any comments at all.
In
calc.c, in the function compareArrays, you have an off-by-one error. Since array[5] is the powerball, array[0..4] should be the normal numbers. You only compare array[0..3] because the conditions say i since the function declarations in that file do not use the time at all. The #include is only necessary in the corresponding calc.c file, if at all.In
options.c, for each call to scanf you must check the return value to see how many variables have been read successfully. You must only access those variables which have definitely been read. Typically, the code looks like if (scanf("%d", &var) == 1) { ... }.From a usability point of view, it is confusing for the user to type
1 when they mean yes. Make them type the obvious y instead of 1. You can read this using scanf("%c", &answer), declaring char answer.The line
int answer = false is wrong, since false is not an int, at least not colloquially. The C compilers only accept this code for historic reasons.In the
Makefile, you should include the options -Os and -Wextra.Context
StackExchange Code Review Q#160559, answer score: 7
Revisions (0)
No revisions yet.