---
title: Deciding by playing at random: Monte Carlo and Hex
url: https://jarroba.com/en/deciding-by-playing-at-random/
author: Ramón Invarato
date: 2026-11-23
modified: 2026-11-23
language: en
license: CC BY-NC-SA 3.0 (https://creativecommons.org/licenses/by-nc-sa/3.0/) — attribution: "Deciding by playing at random: Monte Carlo and Hex", Ramón Invarato, jarroba.com
categories: Artificial intelligence
tags: ai, AI course, Artificial Intelligence, games, Hex, MCTS, Monte Carlo
series: Level 0 — How a machine sees the world (part 8 of 9)
---

# 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.

To play Connect 4, the machine in [Playing against a machine](https://jarroba.com/en/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](https://jarroba.com/tools/en/search?tab=game&game=hex&size=7&sims=10000)**. 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.

[![The 7×7 Hex in the Search tool after the machine starts with 10,000 random games: its red stone near the center, the free cells in darker or lighter purple and, on the other four of the five most played, 61, 60, 59 and 60; the fifth is the chosen one, under its stone. Below, boxed: 10,000 random games, 61% of those games were won by the machine after the chosen move and 10,001 positions stored in its tree](https://jarroba.com/wp-content/uploads/2026/11/hex-7x7-10000-www.jarroba.com_-1-1024x931.png)](https://jarroba.com/wp-content/uploads/2026/11/hex-7x7-10000-www.jarroba.com_-1.png)

Now choose **100** in **Random games per move** (or open [Hex with 100 random games](https://jarroba.com/tools/en/search?tab=game&game=hex&size=7&sims=100)) 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.

[![Comparison of the move chosen with 100 and with 10,000 random games in the 7×7 Hex, on an axis from 0 to 100%. With 100 games, the chosen move won 3 of 3: a red dot at 100% and a long band from 44 to 100%. With 10,000, it won 225 of 366: a green dot at 61% and a short band from 56 to 66%. Below: the band is what that move may really be worth with 95% confidence; 100% looks better and is far less certain](https://jarroba.com/wp-content/uploads/2026/11/100-frente-a-10000-www.jarroba.com_-1-1024x271.png)](https://jarroba.com/wp-content/uploads/2026/11/100-frente-a-10000-www.jarroba.com_-1.png)

## 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](https://jarroba.com/en/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](https://en.wikipedia.org/wiki/Monte_Carlo_method)).

[![Drawing of the Monte Carlo method: three moves, A, B and C, and from each one six random games that end in "win" or "lose". A wins 4 of 6, 67%, and is boxed in green; B, 2 of 6, 33%; C, 3 of 6, 50%. Below: the one that wins the most is chosen; with six games per move chance still weighs in, and with thousands the percentage settles](https://jarroba.com/wp-content/uploads/2026/11/monte-carlo-www.jarroba.com_-1-1024x377.png)](https://jarroba.com/wp-content/uploads/2026/11/monte-carlo-www.jarroba.com_-1.png)

**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:

1. **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.
2. **Expansion:** it adds a new move to its tree.
3. **Simulation:** from there, it finishes the game at random.
4. **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](https://en.wikipedia.org/wiki/Monte_Carlo_tree_search)). 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](https://en.wikipedia.org/wiki/Hex_(board_game))).

**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](https://en.wikipedia.org/wiki/Monte_Carlo_tree_search)).

You can see it in [the tool’s Go](https://jarroba.com/tools/en/search?tab=game&game=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](https://jarroba.com/tools/en/search?tab=game&game=go&size=5) 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 9×9 Go in the Search tool after the machine's first move, with 5,000 random games: its black stone on E3, ringed in gold because it is the chosen one, and, on the free intersections, purple circles that are larger the more games started there, and, on the other four of the five most played, 53 on E8, 52, 51 and 50; the fifth is the chosen one, already occupied by its stone. Below: 5,000 random games, 53% of those games were won by the machine after the chosen move, 5,001 positions stored in its tree, and the line it expects: E3, machine; D7, you](https://jarroba.com/wp-content/uploads/2026/11/go-9x9-5000-www.jarroba.com_-2-727x1024.png)](https://jarroba.com/wp-content/uploads/2026/11/go-9x9-5000-www.jarroba.com_-2.png)

## 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](https://jarroba.com/tools/en/search?tab=game&game=hex&size=5&sims=100) and click **Let the machine start**. It plays the center, which won **86%**, even though the cell above it, to the right, says **100%**.

[![The 5×5 Hex in the Search tool after the machine starts with 100 random games. Boxed, its red stone in the center and, above it, two cells with 83 and 100. Below: 100 random games, 86% of those games were won by the machine after the chosen move and 101 positions stored in its tree](https://jarroba.com/wp-content/uploads/2026/11/hex-5x5-100-www.jarroba.com_-1-1024x946.png)](https://jarroba.com/wp-content/uploads/2026/11/hex-5x5-100-www.jarroba.com_-1.png)

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](https://jarroba.com/tools/en/search?tab=game&game=hex&size=5&sims=1000) and let the machine start. Repeat it with [30,000](https://jarroba.com/tools/en/search?tab=game&game=hex&size=5&sims=30000). What percentage did it win with the chosen move in each case?

With 1,000 games, ___%; with 30,000, ___%.

**Hint:** 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?

- **A.** Because with more games the machine chooses worse.
- **B.** 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.
- **C.** 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](https://en.wikipedia.org/wiki/Monte_Carlo_tree_search): the four steps, the UCT formula and how AlphaGo and AlphaZero used it.
- [Playing against a machine](https://jarroba.com/en/playing-against-a-machine/) and [How far a machine looks](https://jarroba.com/?p=51351), 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.

## Series: Level 0 — How a machine sees the world

1. [Everything is a number (yes, even your name)](https://jarroba.com/en/everything-is-a-number.md)
2. [An image is a table of numbers](https://jarroba.com/en/an-image-is-a-table-of-numbers.md)
3. [A corrupted bit: how to fix it without knowing which one it is](https://jarroba.com/en/when-a-bit-gets-corrupted.md)
4. [What is a model (and why a straight line already counts as one)](https://jarroba.com/en/what-is-a-model.md)
5. [Finding a path: an AI that learns nothing](https://jarroba.com/en/finding-a-path.md)
6. [Playing against a machine: minimax and alpha-beta pruning](https://jarroba.com/en/playing-against-a-machine.md)
7. [How far a machine looks: the horizon effect and deciding with less](https://jarroba.com/en/how-far-a-machine-looks.md)
8. [Deciding by playing at random: Monte Carlo and Hex](https://jarroba.com/en/deciding-by-playing-at-random.md)
9. [How a video game character decides: states, trees and scores](https://jarroba.com/en/how-a-character-decides.md)

