Work the Shell - Deal or No Deal!
I admit it, my kids and I often find ourselves at the local arcade playing video games. There seems to be two basic types of arcades nowadays: those where you play to earn free games and get the high score, and those that are ticket-based where doing well on a game produces tickets that you cash in for cheesy tchotchkes or incredibly cheap stuffed animals.
We tend to go to the latter, and one of the games that's caught my attention is a video version of the popular TV show Deal or No Deal. It got me thinking about odds and probability and how these games actually work.
The game is incredibly simple: you're told that there is a range of prizes hidden in briefcases, and you choose which to eliminate and win what's left in the last briefcase. It's not too exciting, except as you go along, you're offered the opportunity to take a specific cash prize instead of continuing the game.
On the TV show, there are 26 cases, and the prizes range from $0.01 to $1,000,000.00 in uneven jumps. Attractive models hold each case, and the game typically proceeds where the player guesses a bunch of cases to eliminate, in clusters. If the eliminated cases have low-value prizes, that's good—there is, therefore, a better chance of winning big bucks. If they have high-value prizes inside, well, that's not good.
After each set of cases is eliminated, the house offers the player a “deal” to stop playing the game—a payoff that's worth more than the lowest unidentified prize but obviously lower than the highest unidentified prize.
The prizes are 0.01, 1, 5, 10, 25, 50, 75, 100, 200, 300, 400, 500 and 750, and then the big values: 1,000, 5,000, 10,000, 25,000, 50,000, 75,000, 100,000, 200,000, 300,000, 400,000, 500,000, 750,000 and 1,000,000.
Calculating the average payout, assuming you have an equal chance of picking each of the cases at the beginning of the game, is easy: add them up and divide by 26. The result is 131,477.54. If they offered me $130k instead of playing the game? Even though it's a bit lower than the average expected payout, I'd take it. Deal!
Let's say we're most of the way through the game though, and 20 of the possible prizes are knocked out. What's left are 0.01, 50, 300, 1,000 and 250,000. Your chance of picking the 250,000 prize case? One in five.
The expected payout is $50,270, and if that's what you're offered, it's significantly better than four of the five possible outcomes you face. My advice? Deal!
It turns out that there's some sort of random factor that's thrown in to the “deal”, so in the game itself, they vary up or down a percentile value.
To experiment with this as a shell script (yeah, it only took me half the column to get to my first line of code), we need to work with arrays—something that's frankly a bit confusing. Here's how we could define the 26 briefcases:
declare -a cases=(0.01 1 5 10 25 50 75 100 200 300 400 500 750 1000 5000 10000 25000 50000 75000 100000 200000 300000 400000 500000 750000 1000000)
This might be the first time you've seen the declare statement in the shell. In this case, the -a flag declares the variable as being an array. Its usage, however, is not mandatory—arrays can be implicitly declared simply by using the array assignment syntax (a list of values inside parentheses).
You reference individual array elements with $var[index], but there's a twist because of how the shell parses content. What you need to do is actually wrap it with curly braces: ${var[index]}. Add a #, and you get the number of elements in the array, like this:
echo Total number of array elements is ${#cases[*]}
The value that'll be printed is 26, just what we want for Deal or No Deal. To see value #11, you could use ${cases[11]}, but that's wrong. Why? Because shell arrays are indexed starting at zero, so case #11 is actually ${cases[10]}, which, yeah, is pretty confusing.
Let's start by writing the snippet that can calculate expected payout before you have picked a single briefcase out of the collection:
for (( val=0 ; $val < ${#cases[*]} ; val+=1 )) ; do
sum=$(( $sum+${cases[$val]} ))
done
echo sum = $sum, payout = $(( $sum / ${#cases[*]} ))
The bad news? It turns out that the 0.01 value really messes things up, as we can't do integer math with a non-integer value. So, because the 0.01 value actually proves not to influence things much, I'm going to replace it with the value 0. Here's the output:
sum = 3418416, payout = 131477
That's reasonably accurate. Our earlier calculation was 131,477.54. Close enough for gaming work!
Now, let's randomly pick out 22 of the cases, calculate the expected payout and offer a “deal” versus the values still remaining.
First, pick out some cases:
for (( picked=1 ; $picked <= 22 ; )) ; do
pick=$(( RANDOM % 26 ))
if [ ${cases[$pick]} -ne -1 ] ; then
cases[$pick]=-1
picked=$(( $picked + 1 ))
fi
done
That gets all but four cases out of the game by setting their value to -1 (remembering that we're using 0 to represent $0.01). To see what's left in the game and calculate the expected payout of only the remaining values, do this:
for (( val=0 ; $val < ${#cases[*]} ; val+=1 )) ; do
if [ ${cases[$val]} -ne -1 ] ; then
echo \(Still in the game: a prize \
worth \$${cases[$val]}\)
sum=$(( $sum+${cases[$val]} ))
cnt=$(( $cnt+1 ))
fi
done
echo Win \$$(( $sum / $cnt )) if you stop. \
Deal, or no deal\?
exit 0
Run the code, and here's a sample output:
(Still in the game: a prize worth $50) (Still in the game: a prize worth $1000) (Still in the game: a prize worth $5000) (Still in the game: a prize worth $400000) Win $101512 if you stop. Deal, or no deal?
Would you take the deal, or try for the $400,000 case?
Dave Taylor has been involved with UNIX since he first logged in to the ARPAnet in 1980. That means that, yes, he's coming up to the 30-year mark now. You can find him just about everywhere on-line, but start here: www.DaveTaylorOnline.com.
Dave Taylor has been hacking shell scripts for over thirty years. Really. He's the author of the popular "Wicked Cool Shell Scripts" and can be found on Twitter as @DaveTaylor and more generally at www.DaveTaylorOnline.com.
Today’s modular x86 servers are compute-centric, designed as a least common denominator to support a wide range of IT workloads. Those generic, virtualized IT workloads have much different resource optimization requirements than hyperscale and cloud applications. They have resulted in a “one size fits all” enterprise IT architecture that is not optimized for a specific set of IT workloads, and especially not emerging hyperscale workloads, such as web applications, big data, and object storage. In this report, you will learn how shifting the focus from traditional compute-centric IT architectures to an innovative disaggregated fabric-based architecture can optimize and scale your data center.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
| Non-Linux FOSS: Seashore | May 10, 2013 |
| Trying to Tame the Tablet | May 08, 2013 |
| Dart: a New Web Programming Experience | May 07, 2013 |
- RSS Feeds
- New Products
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Drupal Is a Framework: Why Everyone Needs to Understand This
- A Topic for Discussion - Open Source Feature-Richness?
- Home, My Backup Data Center
- Developer Poll
- Dart: a New Web Programming Experience
- What's the tweeting protocol?
- New Products
Enter to Win an Adafruit Prototyping Pi Plate Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Prototyping Pi Plate Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- Next winner announced on 5-21-13!
Free Webinar: Linux Backup and Recovery
Most companies incorporate backup procedures for critical data, which can be restored quickly if a loss occurs. However, fewer companies are prepared for catastrophic system failures, in which they lose all data, the entire operating system, applications, settings, patches and more, reducing their system(s) to “bare metal.” After all, before data can be restored to a system, there must be a system to restore it to.
In this one hour webinar, learn how to enhance your existing backup strategies for better disaster recovery preparedness using Storix System Backup Administrator (SBAdmin), a highly flexible bare-metal recovery solution for UNIX and Linux systems.




49 sec ago
1 hour 34 min ago
3 hours 11 min ago
5 hours 9 min ago
5 hours 26 min ago
5 hours 56 min ago
5 hours 56 min ago
5 hours 57 min ago
8 hours 58 min ago
17 hours 24 min ago