### Table of Contents

## Numerical Analysis of Deal or No deal

This article is a detailed analysis of the mathematics involved in the TV show Deal or no Deal. Anyone who is not already familiar with this TV show should read article Deal or no Deal uk tv show prior to looking at this article, for a description of how it works.

I cannot begin to tell you what a fan I am of this show. I hear so many people say “Oh, that – it’s just opening boxes, how boring can you get”. Comments like that both amaze and annoy me at the same time. It is like saying a game of chess is “just moving pieces around” or Tchaikovsky’s Romeo and Juliet overture is “just a load of notes”. These people have clearly completely missed the complexities and the subtlties of this wonderful game. Yes, it is true that all you have to do is say “Deal” or “No Deal” after each round, but how do you make this decision? The mathematics are complicated, and I will be investigating these in this article. But layered on top of that are the hopes and dreams of the contestant, and the psychology of the situation (typified by the “only here once” attitude). I have another article on devtome (Deal or no Deal box 23) which talks a bit more about the psychology of the game, although it has to be said that the psychology of Box 23 is somewhat different to the psychology of the main game. I may well write about the psychology of the main game in another article, but in this article I will be concentrating on the mathematics of the game. What conclusions can we draw about whether or not to deal based on the hard facts of the statistically outcomes?

At each round in the show (every 3 boxes opened, or 5 boxes in the case of the first round), the contestant is given a financial offer by the banker, and has to make the decision whether to accept the offer or carry on opening boxes in the hope of a better offer later on. It is also possible to carry on until all boxes are opened in which case the contestant will win the amount of money in their own box.

Very often you hear comments like “It’s a good board, there’s another round in it” made either by the player, or by one of the contestants in the wings. However, these comments are made simply on the basis of gut feeling, since the mathematics of working out whether the offer is likely to increase or descrease in the next round is quite complex.

I decided to investigate the actual likelihood of an improved offer in the next and subsequent rounds via a computer analysis. This article describes the techniques used and the results obtained.

## Banker’s Offers

The first point to make is about the bankers offers themselves. As Noel Edmonds is fond of pointing out, there is no rate card. The amount the banker offers a player for a given board configuration depends on a number of factors including the banker’s mood, and the banker’s assessment of what needs to be offered for that particular player. The banker is always interested in selling the box for the minimum amount and the amount he has to offer to achieve this will be less for some contestants than it will be for others.

Having said this, there is a normal range in which the banker makes his offer. You hear the players discussing that the Banker’s normal range is around “half the average”. Well, we get the gist of what is meant by this (and I shall even use similar terminology later in this article) but it is slightly inaccurately put. For our mathematical analysis we need to state what this means precisely.

Consider the blissful situation (known as the dream finish) where the player is left with the largest two boxes at the end of the show. The average of £100000 and £250000 is £175000. Half the average is in this case £87500 and clearly the offer is not going to be in this area for this scenario (there is no incentive to deal at a value below the minimum box value). A more accurate way of describing the Banker’s offers is that they tend to be “The value of the smallest remaining box plus about half the difference between the average and this smallest box amount”. So for the scenario just described, the offer would be

£100000 + (0.5 * (£175000 - £100000)) = £137500

This factor of 0.5 I will refer to as the “banker rate”, because as we shall see, the banker rate is not fixed (even for any one player)

At this point I need to make the following further points here about how the banker pitches the level of each offer given.

- Assessment of the individual player. Depending on the personal circumstances of the contestant, they might be more inclined to deal for a smaller offer than some other players might hold out for. It is really one of the main jobs of the banker to work this out. How much is the player prepared to take a risk? How much do they really need the money being offered? Will they be an OHO?
^{1)}

- A so-called “vulnerable” board. This is a board where many of the larger amounts have already been discovered, but there are 1 or maybe 2 larger amounts still on offer. If for example the £250000 is still present, along with 7 blues, the average will still be around £31500, but the banker is unlikely to offer half the average in this case. He will most likely make the point that there is a near 40% chance of taking out the £250000 in the next round, and that if they player wants to go away with any significant money they had better take the offer given. I would guess the offer might be in the region of £8000 to £9000 in this case, so perhaps around 25% of the average, and could even be less for some players.

- Conversely, if there is not much variation in possible outcomes the banker is likely to offer much closer to the average. Consider the situation at the end of the game, if the 2 boxes remaning are £15000 and £20000. In this sort of situation, there is not much point in offering 25% of the average (which would be £15625). There is very little incentive for the player to take this offer, and so the banker is likely to offer something very much closer to the average (an offer of £17000 plus might be typical for this situation).

From the 3 points above, it is very difficult for us to take account of the first one within our analysis. In any case, from my experience of watching the show, the extent to which this is a factor is in many cases rather small.

However, any statisticians reading this will realize that what is being described by the second two points relates to the variance of the data set. This we can cater for in our analysis. For each combination of possible outcomes we need to look at the mean, and the standard deviation ^{2)} of the data. Then, instead of saying that the banker rate is fixed and around 0.5, we can calculate the banker rate as

Banker rate = Mean / (Mean + Standard deviation)

Let’s see what this means for the scenarios we have already discussed. For the dream finish we have

Mean = 175000 Standard Deviation = 75000 Banker Rate = 175000 / (1750000 + 75000) = 0.7

So the offer should be around

£100000 + (0.7 * (£175000 - £100000)) = £152500

This seems much more like the figure the banker might offer in this case. Let’s also think about the scenario in which there are 7 blues and the £250000. Approximately we have

Mean = 31500 Standard deviation = 82500 Banker Rate = 31500 / (31500 + 82500) = 0.27

So the offer will be (assuming a low blue, effectively £0 as the minimum amount)

£0 + (0.27 * (£31500 - £0)) = £8704

Finally let’s think about the other scenario discussed where the 2 remaining amounts are £15000 and £20000

Mean = 17500 Standard Deviation = 2500 Banker Rate = 17500 / (17500 + 2500) = 0.875

So the offer will be

£15000 + (0.875 * (£17500 - £15000)) = £17187

## Numerical Strategy

The strategy I have employed for this analysis uses a computer to work out all possible combinations of boxes at each round, and to work the offer that the banker might make in each case based on the assumptions as discussed above. From all this collected information we can then work out for any future round, based on the boxes that are still remaining in the game.

- The minimum offer that could happen
- The maximum offer that could happen
- The Average offer
- The median offer

The program I have written does all the above, but I quickly found that the distribution curve of the offers that might be made is not a simple bell curve, and in many cases is very asymmetric. This means that the simple average or median is not all that useful in trying to assess your prospects for the next round. I therefore added a feature to plot a bar chart of number of offers in £1000 ranges. This visual representation is much more useful for making decisions on whether or not to deal.

Please note that the program does not in fact draw the bar chart, but dumps out the raw data in such a way that it can be easily cut and pasted into a spreadsheet which will then draw the bar chart for you.

## Notes on the computer program

The full program is provided at the end of this article for those that may be interested. I will describe here the key points about how it works.

A systematic approach is needed to select all the possible combinations of boxes at each round. First we setup an array (which I have named Boxes) into which we place the contents of each box (from 1p to £250000). Now we need a way of selecting the combinations.

As an example suppose we are selecting the possible combinations when there are only 5 boxes remaining. If you consider the box with 1p as box 1, and the box with £250000 as box 22, then a systematic way of selecting the boxes is as follows. We setup our first selection as the lowest 5 boxes, and gradually increase the box number of our 5th selection.

1,2,3,4,5 1,2,3,4,6 1,2,3,4,7 Etc 1,2,3,4,21 1,2,3,4,22

We now cannot increase the 5th selection any further so we have to increase the 4th selection, and reset the 5th selection to the lowest available value. The next selections are

1,2,3,5,6 1,2,3,5,7 1,2,3,5,8 Etc 1,2,3,5,21 1,2,3,5,22

And then the next selection is

1.2.3.6.7

Eventually we get to

1,2,3,21,22

Now we need to increase the 3rd selection by 1 and retrun the 4th and 5th selection to the lowest available. The next combinations are

1,2,4,5,6 1,2,4,5,7 1,2,4,5,8 Etc

To do all this we could use nested loops, but we need to cater for different points in the game. When there are 5 boxes remaining you would need 5 nested loops (which is bad enough), but when there are 14 boxes remaining you would need a whopping 14 nested loops, which would be unmanageable. So to avoid this we store the selections we are going to make in an array (I have called this selectionArray in the program). In this array we store the set of numbers we chose last time, and we can use the rules described above to work out what the next combination should be.This whole mechanism is handled by the function nextSelection in the code.

Of course, as the game progresses, boxes are opened and so not all combinations are valid any more. When a box is opened we set the corresponding value in the Boxes array to 0.0, and when we are collecting combinations of boxes, we ignore any which contain a box of value 0.0

So the program now has a mechanism to work out all the possible combinations for any round of the game. All we need to do now is translate that into the offer that the banker might make. We use the rule discussed in “Banker’s Offer” section above, and we actually collect every single predicted offer for every single possible combination of boxes, into a big array. In each case we are working out the “banker rate” for that specific combination based on the mean and the standard deviation according to the mechanism discussed. I have dimensioned the array for 1 million entries in the code, but in fact the biggest number of possible offers will be the scenario where we are trying to work out the outcome at 11 box ^{3)} when the game has not yet started (still 22 boxes remaining). There are a little over 700000 different combinations of 11 choices from 22.

The program is written in such a way that it can be used at the same time as watching an episode of Deal or no Deal. You can tell the program which boxes have been selected. You can then ask the program to graph the outcomes (ie the predicted spread of offers) for the next or any subsequent round. You can also ask the comuter for its prediction of the offer that the banker will make based on the current board. This is a good way of determing the level at which the Banker is pitching the offers for this particular player. In the vernacular of the game it is a measure of the “respect” the Banker has for the player.

Even though you can use the program to predict more than 1 round ahead, obviously the further ahead you try and predict, the less valuable the results will be. When playing Deal or no Deal, the focus is normally on what will happen in the next round, and that is quite well addressed by this analysis.

## Analysis of chances each round at the start of the game

The board is always the same at the start of the games, as follows

1p £1000 10p £3000 50p £5000 £1 £10000 £5 £15000 £10 £20000 £50 £35000 £100 £50000 £250 £75000 £500 £100000 £750 £250000

### 17 box

Let’s use the program to see what our chances are at the start of the game (in other words before any boxes at all have been opened. We run the program and get the prediction for the position after the first 5 boxes have been removed

% ./deal Action? details 17 <program prints the raw data here which I have loaded into a spreadsheet to draw the following block graph>

This is already very interesting. You can see that the results are not a simple bell curve, but the curve is skewed in favour of higher offers mainly in the range £6000 to £12000. Also there are no predicted offers beyond £12000. There is a cliff edge to the maximum predicted offer on the first round. For anyone that has watched the show enough times, this is a very familiar pattern – the opening offer is indeed very often in this range, and I can only once recall an opening offer in excess of £12000 (to a contestant named Rich who had a famously gung-ho attitude to the game. He was also extremely lucky in his game and I believe had £50000 and £250000 as the final 2 boxes. I can’t recall the exact figure the Banker gave as an offer, but I do recall that it was ** above** the average – unheard of – and this still was not enough to make him deal. He should have done so though – he ended up with the lower amount as his prize).

### 14 box

We can now look a little further into the future – the second round, where 14 boxes are remaining.

Action? details 14 <program prints the raw data here which I have loaded into a spreadsheet to draw the following block graph>

We can carry on see the predictions for each subsequent round, as below

### 11 box

Incidently, on all these graphs, the values on the y axis are not very interesting – they simply relate to the number of possible outcomes within each £1000 band. What is far more interesting is the value on the x axis. and the size of each of the peaks.

In this graph at 11 box we see the peak is beginning to be skewed slightly towards the lower end of the distribution.

### 8 box

### 5 box

As we are progressing to later and later rounds in the game, we see low offers become more and more prevalent, and fewer and fewer possible outcomes relate to big money offers – although the potential winnings (if you are lucky) go up and up.

### 2 box

## Discussion

The graphs for 5 box and 2 box are not very enlightening other than to demonstrate that ending up with a blue is quite a likely result, and this is something which we see time after time watching the program. The results across all these graphs support the pattern that is often seen in which a good opening offer gradually dwindles away from round to round until eventually the player ends up winning a blue amount.

## Examples of real games

Now let’s see what happens if we use the program during the course of a real game, and whether the information it produces would be useful in deciding whether to deal or continue. Of course this analysis can never be available to a real player of the game, but it is possible that the computer may pick up patterns where it is advisable to deal where the intuitive feel of the board might suggest otherwise (and vice versa).

To this end I have watched a few episodes of Deal or no Deal, and run this program at the same time to see how it fares.

## Mandy’s Game 11/04/2014

This game is not specially selected. I started writing this article 3 days ago and have spent several hours that should really have been spent doing other things putting together the computer program and writing the first part of this article. So this game on 11th April 2014 is the first opportunity I have to try out the program.

The fiirst 5 boxes were as follows - £15000, £50000, £1, £10 and 50p. The computer prediction of the banker’s offer was in the range £9000 to £10000 and the actual Banker’s offer was £9000. This is in fact in the biggest spike of the chart for 17 box which I showed before (this chart is always the same because the same 22 boxes are always present at the start of the game). So this is not a surprising offer in any way.

1p £1000 10p £3000 £5000 £10000 £5 £20000 £50 £35000 £100 £250 £75000 £500 £100000 £750 £250000

I entered the boxes selected into the program and checked the 14 box chart. It was as follows.

This looks promising – it seems the next offer is likely to be higher than the £9000 currently offered. There are 680 possible combinations at 14 box and all bar 32 of them result in an offer of at least £5000 and the biggest peak is at the high end of the possible next offers. No-one ever goes at the first offer, and in this case at least that seems to be backed up by the analysis.

The next boxes were as follows - £50, £20000, £5000, and the board now looks like this.

1p £1000 10p £3000 £10000 £5 £35000 £100 £250 £75000 £500 £100000 £750 £250000

The banker’s offer was £ 10500. This compares with the computer prediction for the remaining combination of boxes of £11000 to £12000. It seems that the banker is producing slightly low offers for this player. The chart produced by the program for 11 box was as follows

It is still looking extremely good, in fact even better than before. Most of the outcomes are £7000 plus. The decision to continue (which was the player’s choice) seems sound.

The next boxes boxes were as follows - £3000, 10p, £1000 resulting in a Banker offer of £15500. I love it – this is right in the middle of the biggest spike predicted by the program at the previous round. It also corresponds exactly with the computer prediction of the banker offer for this configuration of boxes ^{4)}. The board is currently looking like this

1p £10000 £5 £35000 £100 £250 £75000 £500 £100000 £750 £250000

Let’s have a look at the prediction for 8 box. The chart is showing a lot of £20000 plus combinations and plenty of big spikes at £11000 plus.

I asked the program to graph the percentage of offers greater than each level at 8 box

We can see from this graph that all offers at 8 box (100%) will be greater than £1000, but fully 80% of outcomes for 8 box give £11000 or more for an offer. We also appear to have about a 30% chance of £20000 or more at the next offer. The player made the same choice that I would have done in this case and carried on. “No Deal”.

The next Boxes were - £100000, £750, £35000.

1p £10000 £5 £100 £250 £75000 £500 £250000

Clearly this is a setback – two of the Player’s power 5 ^{5)} were removed in this round, and he came up with an offer of £9000. However, the computer prediction of the offer based on the current board is still £14000 to £15000, so it looks like the banker is taking the opportunity to make a harsh offer and scare the player.

The program is showing the 5 box prediction as follows.

This is very interesting – the next round will be critical. At 5 box there are 56 possible combinations, and in this game they are fitting neatly into 4 different groups of outcome. We can also see it with the percent graph. Over 60% of offers will be at least £17000 in the next round.

At this point the player took the Banker’s offer (only £9000). I wonder if she would have done so if she had had the benefit of the information provided by this program? The computer has shown that the offer was low, based on the board, and that the prospects for the next round were still good. You have to admire what the banker has done – he has succeeded in scaring the player into dealing by suggestion through a low offer that the tide of the game has turned. This is not a tactic that would always work for the Banker – many players, particularly young male players, find it impossible to deal on a game in which the offers are decending. However, in this case the Banker read the player perfectly and got the deal done at a very favourable rate.

As always in this show, the game carries on after the player has dealt so we can see what would have happened (and the banker continues to make offers as though the game were still live). The next boxes selected were £5, £75000, and 1p and the banker came up with an offer of £16000 which is neatly in the predicted 3rd block from the previous graph, although the computer prediction for this board was slightly higher at £18000 to £19000 (still just within this range as predicted from the previous round). The Banker is playing on with honesty and the offers are still slightly below the normal level, consistent with what the Banker has done with this player throughout the game.

£10000 £100 £250 £500 £250000

For the sake of completeness I will include here the prediction for 2 box, which is as follows.

In truth the computer program is no longer really necessary. There are only 10 possible outcomes going from 5 box to 2 box, so you don’t really need the computer to tell you what they are.

The results are completely polarized, with 60% of outcomes being £3000 or less, and 40% are massive wins. The program can’t really help you here. If you want to win big at Deal or no Deal, you are going to have to take a big risk at some point, and clearly the risk here is very large. It would take a brave player that was able to turn down £16000 here. As it happened the next boxes were £10000, £500 and £250. And so the final 2 boxes left were £100 and £250000. The Banker made an offer of £50000, but at the time mentioned that £67000 (nearly matching one of the spikes in the prediction) would be normal offer here. However, he had decided that for this particular player he could have persuaded them to deal at a lower rate.

## Conclusion on Mandy’s Game

Mandy dealt at 8 box on £9000. This was a triumph for the Banker, and the computer evidence was showing that the odds were in favour of carrying on to 5 box, which would as it turned out have yielded a £16000 win. As we know, an even bigger win was possible if she had continued to 2 box, but the statistics provided by the program also show what a huge risk this would have been.

Since the £250000 was left at 2 box, the swap was offered and Mandy indicated that she would have swapped her box. This was duly done and the big prize revealed. Oh well!

## Wally’s game 13/04/2014

The next game for me to try the program out on was Wally’s game on the 13th April. Wally was a bit of a character and a retired karate instructor.

The first 5 boxes were as follows, £1000, £20000, £750, £50, 50p. The computer prediction is for a banker offer in the range 10000 to 11000. At this point the Banker did what he sometimes does and produced a joke offer – in this case the cost of a train ticket back to Stockton on Tees - ie £125. No-one ever goes at the first offer in any case, so it matters very little if the banker plays around at the start of the game. Out of curiosity I still had a look at the prediction for the next round, as follows

It is looking good for the next round. On went the game, and the next boxes were 10p, £10000, £250. The computer is telling me that the banker’s offer should be in the £13000 to £14000 range for this board – but bearing in mind the first offer, I wondered what the Banker would actually come up with. The actual offer was £12500, so normality has been restored. It was not enough to tempt Wally however. Here is the prediction for the next round

The boxes taken in the next round were £250000, £5000, and then £75000.

Ok – the £250000 has gone, and also the £75000, another of the Banker’s power 5 – the computer thinks that the offer at this stage should still be in the range £7000 to £8000, and the Banker in fact came up with £6250 so just slightly under the computer prediction.

There was No Deal, and in the next round the boxes taken were £5, £10, and £1. An all blue round, and so the computer prediction for the offer is the top end of the graph above £11000 to £12000. The Banker’s offer was £14500. Wow, the Banker must be worried . The board is looking as follows

1p £3000 £15000 £35000 £100 £50000 £500 £100000

The computer prediction for the next round is as follows

There are a wide variety of possible outcomes now. It really does look like almost any offer between £1000 and low £20000’s is possible in the next round. The bulk of the possibilities seem to be in the £8000 to £16000 range, but there are significant spikes either side of this. Looking at this information, and considering the generosity of this offer (above the computer prediction) I would have seriously considered taking this offer. The likely upside is not that great, and the possible downside is large. However, Wally did not take it.

The next boxes to be opened were £500, £100000, and £15000

1p £3000 £35000 £100 £50000

The computer now thinks the offer should be in the £8000 to £9000 range, and £9000 is exactly what the Banker offered. Without thinking too much about it, Wally decided to No Deal. Let’s look at the prediction for 2 box.

Again – to me Wallyy’s decision does not look wise. 30 percent of outcomes will yield a sub £1000 offer and 80% of outcomes are less than £13000. With an offer of £9000 on the table, taking it would seem like a good move.

Wally was unfortunate for the next 3 boxes to be as follows - £50000, £3000, and £35000

The 2 boxes remaining were 1p or £100. The Banker was celebrating and the audience were booing, but sad to say there was fully a 30% chance of this happening. Poor old Wally. The Banker’s offer of £45 was turned down, and Wally joined the 1p club.

Except he didn’t, because now there is now Box 23 in the game - see article Deal or no Deal Box 23. For obvious reasons Wally decided to open box 23 and lo and behold, it contained +£10000 so Wally actually went away with £10000.01. A good result after all, but if he had been able to follow the advise of this computer program maybe he would have gone away with £14500. This was the first point at which the computer evidence was suggesting that dealing might be a good idea, and it would also have been a banker spanking. ^{6)}

## Nabeela’s Game 14/04/2014

I had by this stage analysed 2 games with very different outcomes but I still felt that another example should be looked at to see how the program was helping. I decided to use the program on the following day.

The first boxes out were £75000, £500, 10p, £15000, and £10000. A fairly average start, resulting in a computer prediction of a fairly average £8000 to £9000 offer. Banker mentioned that he has noted that Nabeela indicates with the desk light that she deals early, and so he will keep all his offers low, but at this point he offers £7000.

The prediction for the next round is suggesting that it is fairly safe to continue, but that a large offer in the next round is unlikely. At this point I decided to do something I had not done to this point, and look at the current prediction for 11 box (2 rounds away) to see if that would help

This is not giving much direction either. We are too early in the game, so will have to wait for further developments. The next boxes selected were £35000, £3000, and £750. Even though 1 of the pwer 5 went in this round, the computer suggestion is that the offer should go up slightly and be in the range £9000 to £10000. Interestingly the Banker only offered £5000. If we look at the current prediction for the next round we can see what a lousy offer that was.

The offer was quickly dismissed with a No Deal. The next boxes out were £50, £10 and £5000. The computer suggestion for the offer was £13000 to £14000, but the Banker is still being a bit tight with an offer of £10000. This is backed up by the computer prediction for the next round

I also created the Percent chart at this point. This chart gives the Percentage of offers which will be above a given level in the next round

We see from this that about 85% of offers will be over about £8000 to £9000 and so a £10000 offer does not look at all appealing. The chances of crashing beneath £4000 are very small indeed. Nabeela decided to continue and I was finding easy to believe that this was a correct decision.

The boxes in the next round were 1p, £1000, and £250. A very good round and the computer is happy to suggest an offer of £20000 to £21000. And £20000 was the Banker’s offer. The state of the board was currently as follows

50p £1 £5 £20000 £100 £50000 £100000 £250000

When I looked at the computer prediction for the next round I got a bit of a surprise.

Everything seems to have been going in the player’s favour over the last couple of rounds, but looking at this chart, the possibilities for the next round are widely, and evenly spread from £5000 all the way up to about £40000. It is not obvious at all which way it is going to go now. It was time to look at another percentage chart

This is showing the same information in another way. There is a near linear decline in the percentage of offers above the x axis amount, and the current offer of £20000 is around the 50% mark. I would say that the computer is delivering a warning shot here. Things could go in either direction now, and I would be very seriously considering taking the £20000. I was quite happy when Nabeela decided to deal at this point.

The boxes in the next round were £20000, 50p and £100. The computer is suggesting an offer of £37000 to £38000, and the Banker actually offered £40000.

I created the chart for 2 box. The range of possibilities is now so spread I even had to change the scale to show ranges of £3000 in size instead of the usual £1000 to make the chart a more manageable size.

Anything from about £2 to about £153000 is now possible. 50% of outcomes are £60000 plus. On the other hand 50% of outcomes are more than £13000 less than the current offer, with 30% less than £15000. What would you do? I have no idea what I would do.

The boxes for the final round were £5, £250000, and £50000. The boxes remaining on the table were £1 and £100000 which in the view of the computer meant an offer of £25000 to £26000. £26000 is an offer known as the “Guv’ner” in the game, so of course the Banker offered that. Nabeela was by this stage convinced that the £100000 was in her box, and so it proved to be.

Box 23 was still there of course, but I was not surprised when this was turned down, and she went away happy enough with her £20000.

## Program

#include <stdio.h> #include <math.h> #define TOTAL_BOXES 22 double Boxes[TOTAL_BOXES+1] = {0.0, 0.01, 0.1, 0.5, 1.0, 5.0, 10.0, 50.0, 100.0, 250.0, 500.0, 750.0, 1000.0, 3000.0, 5000.0, 10000.0, 15000.0, 20000.0, 35000.0, 50000.0, 75000.0, 100000.0, 250000.0} ; static int selectionArray [TOTAL_BOXES+1] ; static double AllOffers[1000000] ; static int selRange = 1000 ; static int actualNumBoxes = TOTAL_BOXES ; static int numBoxes = TOTAL_BOXES ; int offerSort (double *first, double *second) { if (*first < *second) return (-1) ; else return (1) ; return (0) ; } void initSelectionArray() { int i=0 ; for (i = 1; i <= TOTAL_BOXES; i++) { selectionArray[i] = 0 ; } for (i = 1; i <= numBoxes; i++) { selectionArray[i] = i ; } } void initAllOffersArray() { int i=0 ; for (i = 0; i <= 1000000; i++) { AllOffers[i] = 0.0 ; } } int nextSelection() { int box = 0; // start by increasing the last box in the selection array selectionArray[numBoxes]++ ; // if the last item in the selection array has gone above TOTAL_BOXES // then need to increase the selection position before that // and possibly earlier positions too for (box=numBoxes; selectionArray[box] > TOTAL_BOXES - numBoxes + box;) { selectionArray[--box]++ ; } // have we exhausted all combinations? if (selectionArray[1] > TOTAL_BOXES - numBoxes + 1) return 0; // need to setup all positions in the selection array // according to the earliest one we manipulated for (box = box + 1; box <= numBoxes; box++) { selectionArray[box] = selectionArray[box - 1] + 1 ; } return 1; } int checkValidSelection() { int i=0; // if the box contains 0.0 this means already opened // so not a valid choice for (i=1;i<=numBoxes;i++) { if (Boxes[selectionArray[i]] == 0.0) return 0 ; } return 1 ; } double getOffer() { double total = 0.0 ; double min = 250000.0; int i = 0 ; for (i=1; i<=numBoxes; i++) { total += Boxes[selectionArray[i]] ; if (Boxes[selectionArray[i]] < min) min = Boxes[selectionArray[i]] ; } double Avg = total / numBoxes ; double variance = 0.0 ; double totVariance = 0.0 ; for (i=1; i<=numBoxes; i++) { variance = (Boxes[selectionArray[i]] - Avg) * (Boxes[selectionArray[i]] - Avg) ; totVariance += variance ; } double sd = sqrt (totVariance / numBoxes) ; double bankerPercent = Avg / (Avg + sd) ; // banker offers the minimum amount plus his percentage (usually about 50%) // of the difference between the average and the minimum double offer = min + ((Avg - min) * bankerPercent) ; return (offer) ; } getResult (int detail) { // init the selection Array initSelectionArray () ; // init the AllOffers Array initAllOffersArray () ; double totalOffer = 0.0 ; double thisOffer = 0.0 ; int numOffers = 0 ; double lowestOffer = 250000.0 ; double highestOffer = 0.0 ; int result = 1 ; while (result) { int valid = checkValidSelection() ; // Print the selection if (valid) { thisOffer = getOffer() ; AllOffers[numOffers++] = thisOffer ; if (thisOffer < lowestOffer) lowestOffer = thisOffer ; if (thisOffer > highestOffer) highestOffer = thisOffer ; totalOffer += thisOffer ; } // get the next selection result = nextSelection () ; } if (!detail) { printf ("%d: ", numBoxes) ; if (numOffers > 10) { printf ("Low: %f ", lowestOffer) ; printf ("High: %f ", highestOffer) ; } } // sort the array of offers int (*sortfn)() = offerSort ; qsort (AllOffers, numOffers, sizeof(double), sortfn) ; if (!detail) { if (numOffers <=10) { int k=0; for (k=0;k<numOffers;k++) { printf ("%f ", AllOffers[k]) ; } printf ("\n") ; } else printf ("Median %f\n", numOffers, AllOffers[numOffers/2]) ; } if (detail == 1) { int offerRange[250000] ; int minRange = AllOffers[0] / selRange ; int maxRange = AllOffers[numOffers-1] / selRange ; int i = 0 ; // clear the graph data first for (i=0; i<250000; i++) { offerRange[i] = 0 ; } // now collect the graph data for (i=0; i<numOffers; i++) { int range = AllOffers[i] / selRange ; offerRange[range]++ ; } // and print it out for (i=minRange; i<=maxRange; i++) { printf ("%d - %d\n", i * selRange, (i+1) * selRange) ; } for (i=minRange; i<=maxRange; i++) { printf ("%d\n", offerRange[i]) ; } } if (detail == 2) { int offerRange[250000] ; int minRange = AllOffers[0] / selRange ; int maxRange = AllOffers[numOffers-1] / selRange ; int i = 0 ; // clear the graph data first for (i=0; i<250000; i++) { offerRange[i] = 0 ; } // now collect the graph data for (i=0; i<numOffers; i++) { int range = AllOffers[i] / selRange ; int j = 0 ; for (j=range; j>= minRange; j--) { offerRange[j]++ ; } } // and print it out for (i=minRange; i<=maxRange; i++) { printf ("%d - %d\n", i * selRange, (i+1) * selRange) ; } for (i=minRange; i<=maxRange; i++) { printf ("%.2f\n", offerRange[i] * 100.0 / (double) numOffers) ; } } } void chooseBox(char *box) { if (!strcmp (box, "1p")) { Boxes[1] = 0.0 ; actualNumBoxes--; } else if (!strcmp (box, "10p")) { Boxes[2] = 0.0 ; actualNumBoxes--; } else if (!strcmp (box, "50p")) { Boxes[3] = 0.0 ; actualNumBoxes--; } else { double amount = (double) atoi (box) ; int i = 0 ; for (i=4;i<=TOTAL_BOXES;i++) { if (Boxes[i] == amount) { Boxes[i] = 0.0 ; actualNumBoxes--; return ; } } printf ("Unrecognised or already selected: %f\n", amount) ; } } double getBankerRate (double offer) { double total = 0.0 ; double min = 250000.0; int i = 0 ; for (i=1; i<=TOTAL_BOXES; i++) { total += Boxes[i] ; if (Boxes[i] < min) min = Boxes[i] ; } double Avg = total / numBoxes ; // banker offers the minimum amount plus his percentage (usually about 50%) // of the difference between the average and the minimum double rate = (offer - min) / (Avg - min) ; return (rate) ; } int main(int argc, char **argv) { char buffer[200] = "" ; while (1) { printf ("Action?\n"); gets(buffer) ; if (!strncmp (buffer, "box", 3)) { chooseBox (&buffer[4]) ; } else if (!strncmp (buffer, "banker", 6)) { double bankerOffer = (double) atoi (&buffer[7]) ; double rate = getBankerRate(bankerOffer) ; printf ("Banker Rate is %f percent of the average\n", rate*100) ; } else if (!strncmp (buffer, "detail", 6)) { numBoxes = atoi (&buffer[7]) ; getResult (1); } else if (!strncmp (buffer, "range", 5)) { selRange = atoi (&buffer[6]) ; } else if (!strncmp (buffer, "percent", 7)) { numBoxes = atoi (&buffer[8]) ; getResult (2); } else if (!strncmp (buffer, "summary", 7)) { numBoxes = actualNumBoxes - 1 ; while (numBoxes >= 2) { if (numBoxes == 14 || numBoxes == 11 || numBoxes == 8 || numBoxes <=5) { getResult(0) ; } numBoxes--; } } } return 0; }

^{1)}

^{3)}

^{4)}

^{5)}

^{6)}