CS1101C Lab 3 (Even Week)
4-D Draw
The deadline for this lab question is Wednesday 19 October 2005,
23:59:59 hours.
The name of your C program file must be called fourd.c,
files with any other name will not be marked.
Background
As an educated Engineer-to-be or Scientist-to-be, you have long known or
suspected that the 4-D lottery conducted by Singapore Pools (as with all
other gambling devices) is one big scam to get people to part with their
hard-earned money. Determined to prove this, you decide to do a very
simple simulation of the 4-D lottery.
4-D Lottery
A person chooses a “lucky” 4-digit (4-D) number from 0000 to
9999. Clearly, there are 10,000 numbers to choose from. If the number
matches any of the 4-digit numbers drawn, the person win a prize. Else,
the person wins nothing. In both cases, the bet amount is forfeited.
The following shows the actual numbers drawn for the Sunday 16 October
2005 4-D draw. Each 4-D number can be drawn for only one
prize.
1st Prize |
2115 |
2nd Prize |
1462 |
3rd Prize |
2552 |
Starter Prizes |
Consolation Prizes |
0694 |
1329 |
1221 |
1240 |
1907 |
2468 |
3827 |
4227 |
3608 |
4029 |
4412 |
5001 |
4641 |
5314 |
6331 |
7198 |
9522 |
9656 |
7830 |
8810 |
There are two main types of bet you can make. You can either make a Big
Bet or a Small Bet. The following table shows the prize money
for a bet of one dollar.
Prize Category |
No. of Prizes |
Big Bet $1 |
Small Bet $1 |
1st |
1 |
$2,000 |
$3,000 |
2nd |
1 |
$1,000 |
$2,000 |
3rd |
1 |
$490 |
$800 |
Starter |
10 |
$250 each |
NIL |
Consolation |
10 |
$60 each |
NIL |
- If you made a Big Bet $1 and you picked the number
4029 in the Sunday 16 October 2005 draw, you win the
Starter prize and you will get $250 in prize money.
- If you made a Small Bet $1 and you picked the number
1462 in the Sunday 16 October 2005 draw, you win the
2nd prize and you will get $2,000 in prize money.
- Bets of more than $1 are allowed. If you made a Big Bet of
$5 and you picked the number 6331 in the Sunday 16
October 2005 draw, you win the Consolation prize and you will
get $60 * $5 = $300 in prize money.
- If you made a Small Bet of $10 and you picked the
number 2115 in the Sunday 16 October 2005 draw, you win
the 1st prize and you will get $3,000 * $10 = $30,000 in
prize money. Time to bring out the champagne!
- There is no Starter prize and no Consolation prize for
a Small Bet.
- Whether you win anything or not, you will forfeit the money that you
paid for the bet. In scenario 3 above, your net gain is $300 - $5 =
$295.
Scenarios
Scenario 1: Suppose you start with $1,000. You decide to
make a Big Bet of $1 for each of the next 1,000 4-D
draws. At the end, will you make money or lose money?
Scenario 2: What about the long term scenario? Suppose you start
with $1,000,000. You decide to make a Small Bet of
$1 for each of the next 1,000,000 4-D draws. At the end,
will you make money or lose money?
Scenario 3: If you are the more adventurous sort, you may wish to
bet larger amounts. Suppose you start with $100,000. You decide
to make a Big Bet of $5 for each of the next
100,000 4-D draws. At the end, will you make money or lose money?
But you have to be careful this time, because you may run out of money
(negative balance). Because you do not want to depend on loan sharks or
banks, you wisely decide to stop anytime your balance drops below your
bet amount. In this case, you will stop whenever your balance drops
below the bet amount of $5.
Is Big Bet or Small Bet generally better in the long
run?
At the end of each scenario, you want to know how much money you have
remaining (the balance), as well as how many actual draws you
participated in. The number of actual draws may be less as you may run
out of money (especially in Scenario 3)!
You would like to have these questions (and many other similar
questions) answered.
Simulation
You will be provided with an input file called “fourd1.txt” consisting of different
simulation scenarios. Each line in the input file gives the parameters
for a single scenario. A typical line from the input file consist of
five integers separated by blank spaces as shown below:
231 0 1000 1 1000
The explanation for each number is given as follows:
231 | Random number seed.
|
0 | Type of bet. 0 means Small Bet, 1 means Big
Bet.
|
1000 | Starting balance.
|
1Bet amount.
| 1000 | Number of draws.
| |
Input File
Assume that the text input file is always called fourd1.txt. This sample input file
contains data for ten scenarios:
231 0 1000 1 1000
232 1 1000 1 1000
233 0 10000 2 10000
234 1 10000 2 10000
235 0 100000 5 100000
236 1 100000 5 100000
237 0 1000000 1 1000000
238 0 1000000 1 1000000
239 1 1000000 1 1000000
240 1 1000000 1 1000000
We will test your program with other experiment data, so your program
must work for the general case, and not just only for the sample input
file given.
Sample Run
The following shows a sample run for the input file fourd1.txt. Some actual values have been
replaced with four x’s. The x’s do not represent the number
of digits in the output.
You are reminded to follow the sample outcome exactly, else marks will
be deducted.
Seed: 231, Small Bet $1, Starting balance: $1000,
Number of Draws: 1000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 232, Big Bet $1, Starting balance: $1000,
Number of Draws: 1000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 233, Small Bet $2, Starting balance: $10000,
Number of Draws: 10000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 234, Big Bet $2, Starting balance: $10000,
Number of Draws: 10000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 235, Small Bet $5, Starting balance: $100000,
Number of Draws: 100000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 236, Big Bet $5, Starting balance: $100000,
Number of Draws: 100000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 237, Small Bet $1, Starting balance: $1000000,
Number of Draws: 1000000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 238, Small Bet $1, Starting balance: $1000000,
Number of Draws: 1000000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 239, Big Bet $1, Starting balance: $1000000,
Number of Draws: 1000000, Ending Balance: $xxxx, Actual draws: xxxx.
Seed: 240, Big Bet $1, Starting balance: $1000000,
Number of Draws: 1000000, Ending Balance: $xxxx, Actual draws: xxxx.
Take Note
- You may assume that the numbers in the file are always integers.
- Set the random number seed only at the beginning of each scenario.
- Use the following rand_float function for more realistic
results.
double rand_float(double a, double b)
{
int num, den;
if (RAND_MAX == 32767) {
num = (rand() << 15) | rand();
den = (1<<30)-1;
return ((double)num/den)*(b - a) + a;
} else
return ((double)rand()/RAND_MAX)*(b - a) + a;
}
Call the rand_float function with:
rand_float(0, 9999.999999)
and assign the result to an integer. This gives you a number between 0
to 9999 inclusive.
Tips
- You have to think intelligently about how you want to tackle this
program. The most complicated program is not always the best program.
Often, a simpler program works just as well and is a lot easier to code
and understand.
- This may be the most complicated program you have ever designed. Go
to the drawing board and plan your program. Think about the individual
modules you want using the divide-and-conquer strategy. Write and test
each module / function individually. Do not ever write the complete
program, and then start compiling / running / testing it. You will end
up with numerous compilation and logic errors which will take a long
time to fix. Remember how I (Raymond) showed in lecture that you should
practice incremental coding by incrementally coding parts of the
program, then testing each part out before moving on to other parts of
the program.
- You should probably not use the input file given when testing
your program for the first time; you should create your own input file
(by using the Unix command “vim fourd1.txt”) with
manageable values, fewer (one?) scenarios, and small number of draws.
When you are confident that your program is working for small number of
draws, increase it. Do the same for the number of scenarios.
- You should use many printf statements to give more
information of what your program is doing exactly. In general, you
should print out as much debugging information as you need; then before
submission, comment out the debugging information.
Ponder
In the long run, how much money does Singapore Pools make for every $1
that you bet for (a) Big Bet (b) Small Bet? Does your experimental
results tally with the analytical results?
This document, index.html, has been accessed 19 times since 25-Jun-24 11:57:13 +08.
This is the 1st time it has been accessed today.
A total of 13 different hosts have accessed this document in the
last 445 days; your host, 216.73.216.39, has accessed it 1 times.
If you're interested, complete statistics for
this document are also available, including breakdowns by top-level
domain, host name, and date.