Since science is all about making things predictable, it is sort of a surprise that many of the advances in science in the last hundred years have been made using mathematics about things which are inherently and intrinsically unpredictable: the mathematics of probability, and its applied-math stepchild, statistics.
The usual example of something that’s inherently unpredictable is flipping a fair coin. Take a quarter from your purse, flip it, and it comes up either heads or tails.
Now, because I know my readers, I can tell someone is getting set to write me a comment about how there’s no such thing as a perfectly fair coin, or that it can also land on an edge, or explaining how they learned to flip a coin so it made exactly one turn and so they could always predict how it would land, so let me just say: this is mathematics, it’s a perfectly fair coin, and we’re going to be catching it in the air so it never lands on edge. So just stop.
As I said, when you flip this perfectly fair coin, it either comes up heads or tails. The next time you flip it, it also comes up either heads or tails, and which comes up doesn’t depend on the previous flip at all. Technically, we’d say it “has no memory”, it’s memory-less. Random things with this memory-less property are going to be important, so remember the word.
The gambler’s fallacy is imagining that something like a fair coin actually has memory — in other words, if you’ve had a run of heads, you’re “due for” tails to come up. The truth is that every time you flip a coin, what comes up is independent of all the previous flips. What makes you think you’re “due for” a tails is that over many coin flips, the likelihood of getting a run of many heads or tails gets smaller, and it gets smaller quickly.
Let’s start with the simplest case. If you flip a coin exactly once, the chances of getting all heads are exactly 50-50. It’s either heads or tails, which we’re going to represent as 0 for heads and 1 for tails. Flip the coin twice, and the chance of getting all heads drops to 1 in 4: 00, 01, 10, 11. Three times, and it’s 1 in 8: 000, 001, 010, 011, 100, 101, 110, 111. I won’t carry out the examples any further, but it’s easily shown that this pattern carries on forever, and the chance of getting a run of heads of length n is exactly 1/2n. Now think about flipping a fair coin many many times: for every run of 10 coin flips, we won’t get a run of 10 heads 1023 out of 1024 times. So you’re right that you learned to expect that you won’t get ten heads in a row; the fallacy is that if you have gotten nine heads in a row, you’re still going to get that tenth heads exactly half the time.