Skip main navigation

New offer! Get 30% off one whole year of Unlimited learning. Subscribe for just £249.99 £174.99. New subscribers only T&Cs apply

Find out more

Practise modifying code

Practise modifying code
Hello everyone. Now, today we’ve got a very special guest with us. We’ve got Dave Spiegelhalter. Now, David will you, for the benefit of people who watch, will you tell us the full title of your job here. I am the Winton Professor of the Public Understanding of Risk in the University of Cambridge. Okay so, try and imagine getting that onto a business card. Now, this is going to be one of the most highly qualified videos ever seen on YouTube with the two of us here. Now we are going to teach you a new game today. This game will help you beat your friends, and then we’re going to help you beat them again.
And then, finally, I’m going to teach you a brand new game. A game that I’ve invented which will help you beat two of your friends at the same time. Now, David I’ve got three coloured dice here, three different dice. They’re not like regular dice. So, we haven’t got the regular numbers on them. So, for example this one here has threes and sixes on it. This dice has twos and fives and this has got ones and fours. So, they’re not regular normal dice. So, first of all I want you to pick one of these dice. Take a look at them, think carefully, pick a good dice for you. I will have the yellow one.
Okay, a yellow one for David and what I’m going to do is I’m going to pick the red dice. Now, we can get rid of this one because we don’t need it for now. So, we’ll get rid of that and what we’re going to do is we’re going to play 10 games. We’re going to roll the dice 10 times and whoever gets the highest number wins. That’s the idea, it’s best of 10. Who can score most out of 10 rolls. So, here we go, here’s game number one. Five. Five I got a three. So, David wins. Two. Two, I’ve got a three. So, we’re Even-Stevens. Five. Five, three. So, David is pulling ahead. Two. Two, three again we’re even. Two.
Two, I’ve got a three. So, I’m ahead now, this is good. Five. Five, I have three. Oh, back to normal. Five. Five, three - David’s coming ahead. Two. Two, three – Even-Stevens. It’s coming down to the wire. Two. Three for me, so I’ve now pulled ahead. Five. Five, and I’ve got a six and if I’m right that’s 10 rolls which means I won, aha! Right, so, what David didn’t know, of course what you didn’t know, is that whatever dice David picks I can always pick a dice with a better chance of beating yours. As we have seen, the red dice will beat the yellow dice in the long run. The yellow dice, in fact, beats the green dice.
So, you might expect then that the red dice will beat the green dice. The red dice is the strong one, the green dice is the weak one. So, we would think that the red dice beats the green dice. And, if this was true, this would be called a ‘transitive game’ because the property is transferred via the yellow dice in the middle. But this is more like rock, paper, scissors. In fact, these go round in a circle. So, the red dice beats yellow dice, yellow dice beats green dice and green dice beats red dice - and this is called a non-transitive game. Now, like I said, you can make these for yourself. I invite you to do so.
But, how does this work? Well I’m going to pass over to David now who’s going to explain. Let’s just look at how we can work out the probability of red dice beating the yellow dice. Now, red actually had five faces with three on it and one face with six on it. So, when we throw the red there’s a 5/6 chance of us getting a three and a 1/6 chance we’re getting a six. Now, when the yellow is thrown that’s got three faces with two on it and three faces with five. So, there’s a half a chance we’re getting a two and a half chance of getting a five and that’s the same no matter what the red was thrown.
And so, when we look at this combination, that was red beats yellow. So, that’s a red win. In this case, yellow will win. In this case, six beats two red will win. In this case, six beats five so, red will win again. So, there’s three possible outcomes where red will win. Now, what’s the probability of one of those occurring. Well, to get the probability of a particular combination we multiply the probabilities along the tree. So, the chance of this combination is 5/6 x 1/2 is 5/12 chance of that happening. This one is 1/6 x 1/2 and that’s a 1/12 chance of that happening. Down here a 1/6 x 1/2 another 1/12.
Now so, the chance of any one of these happening is the sum of these probabilities. We add them up and we get 7/12. The important thing is, that’s bigger than 1/2. So, on average red is going to beat yellow. We can do exactly the same to show that on average yellow will beat green and then the rather remarkable thing is that we can do the same to show that, on average, green will beat red. Since we both know how it works we’re going to play this again. But this time you’re going to be in charge. So, I’m going to be your stooge right. So, well first thing I’m going to have to do is pick one of these colours, right?
So, what I’m going to do is I’m going to pick yellow, right, yellow is my favourite colour. I’m going to pick yellow. And I know the right thing to do so, I’m going to pick red. So, you’re picking red. But I’m going to switch the rules on you, right, and this time what we’re going to do is we’re going to play this game with two sets, with two dice. If we have two dice the incredible thing is that the order of the chain which we saw before where the red dice beats the yellow dice, the yellow dice beats the green dice, the green dice beats the red dice, that chain actually flips. The order of the chain flips around.
So, now the red dice beats the green dice, the green dice beats the yellow dice and the yellow dice beats the red dice. That’s the amazing thing. Now, this isn’t always true. This isn’t true for all sets of non-transitive dice. It’s true for this set. Here’s another set. You can buy these on the internet as well. This is a set of four non-transitive dice, they’re called Efron dice. They’re named after the statistician Brad Efron. And this will work again in a chain in the same sort of way that we saw before. I’ll put up the instructions on the screen but this would not flip with two sets of dice you have to be careful about this.
What we’re going to show you next is a new game that I’ve invented which will mean that you can beat two players at the same time. So, I’m quite excited by this because this is going to be an exclusive. Okay, so this is the world premiere. These are a set of five non-transitive dice here. I’ve colored them in the sort of colours of the rainbow. So, we’ve got Richard of York gave battle in Wayne! So, we’ve got the colours of the rainbow and, as traditional, here we’ve got the red ice beats yellow, yellow beats green, green beats blue, blue beats white and then the white beats red in a big circle, the sort of thing we’ve seen before.
But, in fact, there’s actually a second chain inside this because at the same time red beats green, green beats white, white beats yellow and yellow beats blue, and then blue beats red again. So, there are two chains in this game. But, and this sticks out a bit now, this isn’t always true and I had to pick my dice carefully when I made these but, with two sets of dice, we’ve seen the idea that the chain flips we’ve seen this sort of thing already. Well, in this case, the first chain stays the same. So, for two sets of dice red beats yellow, beats green, beats blue, beats white, beats red again. That’s the same as before. But the second chain flips.
The second chain now flips. So, now we have red beats blue, blue beats yellow, yellow beats white, white beats green and green beats red. So, the idea with this then this should mean that you can play this game with two of your friends. They can pick any colour that they want and you should be able to pick a colour that will beat or have a better chance of beating both of them if you get to pick the rules of the game. By the rules of the game I mean if you can pick if you’re playing a one dice version of the game or a two dice version of the game.
So, for this David has invited along a friend of his. David who’s this? This is the Cambridge Professor of Ornithology. Right, okay. So, we’ve got three players now. Now, first of all this is how it’s going to work. These are my two stooges. They are going to pick a dice for themselves. So, you’re going to pick a colour. So, first of all David which colour would you like? I will choose yellow. And Professor? Oh green, green. So the Professor, he picks green. So, in this case what I’m going to do is I’m going to pick the red dice.
Now, in this case if I just pick the red dice and a single dice version of the game I should expect to beat both professors here. I should beat them both at the same time. But let’s say you pick something else, perhaps you don’t pick that. So, what would you pick this time? Well I’m going to choose white this time. Right. And, oh green again, green. Green again for the professor and I’m gonna pick, in this case, I’m gonna pick yellow. But I’m going to pick the two dice version of the game and, in this case now, I should have a better chance of beating both players. So, do try them out. Do make them.
Post any successes or failures on my comments and until next time, if you have been, thanks for watching.

The Python code we developed for comparing the values of two dice in the previous step can be modified to study a different kind of dice, called Grime Dice (invented by James Grime). Watch the video to find out more about the Grime dice.

We can investigate Grime dice by rolling them many times (in Python) and then doing some simple counting to analyse the data from the rolls.

Grime Dice are regular six-sided dice, but instead of the faces having labels 1,2,3,4,5,6 we’ve given them face labels as follows. The colours of the dice are just to be able to clearly tell them apart from each other.

  • The RED dice has its faces labelled [3,3,3,3,3,6]
  • The BLUE dice has its faces labelled [2,2,2,5,5,5]
  • The GREEN dice has its faces labelled [1,4,4,4,4,4]

The Python code we saw earlier, for comparing the values on two dice (win, loss, draw), has been modified to compare, for example, the RED dice playing against the BLUE dice. For these Grime Dice, there are never any draws because no two dice share a common face label. The NumPy library function np.random.choice randomly selects one of the values in the list of face labels provided. Each dice is rolled 60000 times.

myrollsA = np.random.choice([3,3,3,3,3,6], size=60000)
myrollsB = np.random.choice([2,2,2,5,5,5], size=60000)
mysum_win = np.sum(myrollsA > myrollsB)

Your task

Run the Python code above in your Jupyter Notebook to compare the RED dice with the BLUE dice. Which dice has the most wins from 60000 rolls, RED or BLUE?
Now, modify the code to compare the BLUE dice with the GREEN dice. Which dice has the most wins from 60000 rolls, BLUE or GREEN?
If you now compared the RED dice with the GREEN dice, which do you think would have the most wins from 60000 rolls? Modify the code again to check your hypothesis.
Share your answers to each question and your modified code with your fellow learners in the discussion area. What did you learn from this? Did your modifications work first time? Did your hypothesis prove true?


Grime, J. (n.d.). Non-transitive dice.

Grime, J. (2010, August 5). Non-transitive dice. [Video] YouTube.

This article is from the free online

Get ready for a Masters in Data Science and AI

Created by
FutureLearn - Learning For Life

Reach your personal and professional goals

Unlock access to hundreds of expert online courses and degrees from top universities and educators to gain accredited qualifications and professional CV-building certificates.

Join over 18 million learners to launch, switch or build upon your career, all at your own pace, across a wide range of topic areas.

Start Learning now