Deciding by playing at random: Monte Carlo and Hex
How a machine decides when it cannot evaluate a position: thousands of random games, the Monte Carlo tree (MCTS) and Hex. AI course, 0.8.

AI from the First Bit › Level 0 — How a machine sees the world part 8 of 9
Course modules
- 1Everything is a number (yes, even your name)
- 2An image is a table of numbers
- 3A corrupted bit: how to fix it without knowing which one it is
- 4What is a model (and why a straight line already counts as one)
- 5Finding a path: an AI that learns nothing
- 6Playing against a machine: minimax and alpha-beta pruning
- 7How far a machine looks: the horizon effect and deciding with less
- 8Deciding by playing at random: Monte Carlo and Hex
- 9How a video game character decides: states, trees and scores
To play Connect 4, the machine in Playing against a machine needed someone to write down how much a half-played position is worth. In many games nobody knows how to write that formula. There is another way out, and it sounds like a joke: play thousands of random games from each possible move and keep the one that wins the most.
Try this first
Open the Game tab of the Search tool with Hex. Hex is played on a diamond-shaped board of hexagons. Red tries to connect the top edge with the bottom one, and blue tries to connect the left edge with the right one. It cannot end in a draw: with the board full, one of them has always connected. Click Let the machine start.
The machine plays red, on a cell near the center. Before that, it played 10,000 random games. Each one starts at one of the free cells, and then both sides place stones without thinking, anywhere, until one of them connects. The board shows it: the darker a cell, the more games started there, and on the five most played it writes the percentage of those games it won. With the cell it chose it won 61%: that number is not visible on the board, because its stone covers it, and appears below. The 61 above is from another cell with a similar result, a few tenths lower: it won 216 of its 353 games, 61.2%, against 225 of 366, 61.5%, for the one the machine chose.
Now choose 100 in Random games per move (or open Hex with 100 random games) and click Let the machine start again. It plays on a different cell, next to the right edge, and says it won 100% of the games that started there. That looks better. But it is 3 games, and it won all 3. Play one game against each: the 100-game machine is beaten without much effort; the 10,000-game one is hard.
What just happened
Why minimax is not enough. In tic-tac-toe, the machine looked at every possible game to the end. In 7×7 Hex it cannot: the tool warns that there are more than 10²⁰ positions. In Connect 4 the search was cut off a few moves ahead and there the position was estimated with a hand-written formula that counted the open lines of four (Playing against a machine). In Hex that formula is hard to write: a row of stones can be worth a lot or nothing depending on where the opponent cuts it.
What it does instead. A single random game says little, because both sides play terribly. But if you play thousands from one move and the machine wins many more than it loses, that move leaves it in a position that is easy to win, even if nobody can explain why. This is the Monte Carlo method: computing something hard by repeating a random experiment many times and counting how it turns out. It was named after the Monte Carlo casino (Monte Carlo method, Wikipedia).
The tree. Sharing the games equally among the 49 cells would spend almost all of them on bad moves. That is why the machine builds a tree as it goes, and why the method is called Monte Carlo Tree Search (MCTS). Each random game goes through four steps:
- Selection: it goes down through the moves it has already tried. At each one it chooses with a rule that balances two things: sticking with the ones that are winning and not abandoning the ones it has barely tried.
- Expansion: it adds a new move to its tree.
- Simulation: from there, it finishes the game at random.
- Backpropagation: it goes back up along the same path and records, at each move, one more game and whether it won it.
That is where the 10,001 positions stored in its tree come from: one per game, plus the current one. And that is why the board is not a uniform color: the dark cells are the ones the rule has seen as promising. The rule is called UCT (for Upper Confidence bounds applied to Trees). It was published by Levente Kocsis and Csaba Szepesvári in 2006, the same year Rémi Coulom named the method (Monte Carlo tree search, Wikipedia). Choosing between exploiting what already works and trying what you do not know yet is the same dilemma as in Learning from reward.
More games, better play. I pitted the machine against itself on 7×7, with a different number of random games per move, switching who starts each time:
| Random games per move | Against | Games won |
|---|---|---|
| 1,000 | 100 | 20 of 20 |
| 10,000 | 1,000 | 10 of 10 |
In both cases it knows the same about Hex: its rules and nothing else. Only the number of random games it has played changes.
A game with a history. Hex was invented by the Danish mathematician Piet Hein in 1942, and John Nash reinvented it at Princeton around 1948. Nash proved with a very short argument that the first player can always win with perfect play, but his proof does not say how. Computers have found the way to win up to the 9×9 board; on 11×11, the tournament size, nobody knows it (Hex, Wikipedia).
From Hex to Go. AlphaGo, the program that beat Lee Sedol at Go in 2016 by four games to one, used this same tree, guided by neural networks (Monte Carlo tree search, Wikipedia).
You can see it in the tool’s Go. Click Let the machine start: on the 9×9 board, with 5,000 random games, it opens on E3 and wins 53% of the games that started there, and the other four most played stay between 50 and 53%. At the start of Go, as at the start of Hex, all moves win roughly the same in random games. On the 5×5 board you can also see the flaw of a tree that knows nothing about the game: it chooses C2, next to the edge, and not the center. AlphaGo walked the same tree, but its neural networks told it which moves were worth trying and how much each position was worth.
The classic mistake
You might think the best move is the one with the highest percentage. Open the 5×5 Hex with 100 random games and click Let the machine start. It plays the center, which won 86%, even though the cell above it, to the right, says 100%.
Behind the 100% there are 6 games, all won. Behind the 86%, 7, of which it won 6. With so few games, that difference is chance. The machine chooses the most played cell, not the one with the best percentage: the selection rule only keeps giving games to a cell while it wins, so a cell with many games has had to defend its percentage many times. A percentage computed from few cases is misleading, as happens with a small sample.
Try it yourself
- Play against the machine with 100 random games until you beat it. Then go up to 10,000. Can you beat it?
- Turn on Also analyse my turns: on your turn, the machine plays its random games from your position and tells you how much you would win with the best move. Watch how that percentage changes after a bad move of yours.
- Switch to 11×11 with 30,000 random games. How far apart are the percentages of the five most played cells?
Test yourself
Open the 5×5 Hex with 1,000 random games and let the machine start. Repeat it with 30,000. What percentage did it win with the chosen move in each case?
With 1,000 games, ___%; with 30,000, ___%.
It is the big percentage below the board.
See the answer
Answers: 69 and 61.
With 100 it was 86%, with 1,000 it was 69%, with 10,000 it was 63% and with 30,000 it was 61%: it drops less and less.
Test yourself
Why does the percentage of the chosen move drop the more random games the machine plays?
- Because with more games the machine chooses worse.
- Because with few games luck inflates the percentage of the move that has been winning the most; with many, it gets closer to what it is truly worth in random games.
- Because the opponent in the random games learns and plays better.
See the answer
The answer is B.
It is the same as the 100% before, with only 6 games: a percentage over few games is misleading. In addition, the machine chooses the most played move, and it plays the winning ones more, so with few games it rewards the one that was lucky early on. With many, luck evens out. And at the start of Hex all moves win roughly the same in random games: that is why it stays near 60%, and the tool says so below the board.
Further reading
- Monte Carlo tree search, Wikipedia: the four steps, the UCT formula and how AlphaGo and AlphaZero used it.
- Playing against a machine and How far a machine looks, from the same course: the other way to decide, looking at the moves in order with minimax.
- Learning from reward, from the same course: the same balance between exploiting and exploring, when the machine learns by playing.
“Deciding by playing at random: Monte Carlo and Hex” by Ramón Invarato at jarroba.com, licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0. Registered at Safe Creative.

You may also like
How a video game character decides: states, trees and scores
Video game AI without learning: state machine, behavior tree, utility as in The Sims, and GOAP. AI course, 0.9.
How far a machine looks: the horizon effect and deciding with less
Why a machine that looks only a few moves ahead gives pieces away, how quiescence search saves it and how engines decide by looking at less. AI course 0.7.








Leave a comment