user5633: Is there such a thing as a chess program that at random times can vary it's strength of play between say a beginner and an expert? Or maybe a program that a certain random times makes subtle or obvious mistakes?

user825: Of course, and in fact it's very common.
Stockfish
The code for the implementation:
int weakness = 120 - 2 * level;

// Choose best move. For each move score we add two terms, both dependent on
// weakness. One deterministic and bigger for weaker levels, and one random,
// then we choose the move with the resulting highest score.
for (size_t i = 0; i < multiPV; ++i)
{
    // This is our magic formula
    int push = (  weakness * int(topScore - rootMoves[i].score)
                + delta * (rng.rand<unsigned>() % weakness)) / 128;

    if (rootMoves[i].score + push > maxScore)
    {
        maxScore = rootMoves[i].score + push;
        best = rootMoves[i].pv[0];
    }
}

If you ask Stockfish to play weaker chess, it'd randomly choose a weaker move. It does that by drawing from a uniform distribution and linearly relate the level you choose with the random noise (weakness and push in the code).
SmallChess
Other engines adopt other implementations. For example, I prefer a model where I draw a sudden large jump to simulate a gross blunder. The probability depends on the level, and it is related to the number of pieces on the board. When my engine feels it needs to make a blunder, it'd do a quick shallow search. My intention is try to simulate simple tactic errors.

5633: <p>Is there such a thing as a chess program that at random times can vary it's strength of play between say a beginner and an expert? Or maybe a program that a certain random times makes subtle or obvious mistakes?</p>
 825: <p>Of course, and in fact it's very common.</p>

<p><strong>Stockfish</strong></p>

<p>The code for the implementation:</p>

<pre><code>int weakness = 120 - 2 * level;

// Choose best move. For each move score we add two terms, both dependent on
// weakness. One deterministic and bigger for weaker levels, and one random,
// then we choose the move with the resulting highest score.
for (size_t i = 0; i &lt; multiPV; ++i)
{
    // This is our magic formula
    int push = (  weakness * int(topScore - rootMoves[i].score)
                + delta * (rng.rand&lt;unsigned&gt;() % weakness)) / 128;

    if (rootMoves[i].score + push &gt; maxScore)
    {
        maxScore = rootMoves[i].score + push;
        best = rootMoves[i].pv[0];
    }
}
</code></pre>

<p>If you ask Stockfish to play weaker chess, it'd randomly choose a weaker move. It does that by drawing from a uniform distribution and linearly relate the level you choose with the random noise (<em>weakness</em> and <em>push</em> in the code).</p>

<p><strong>SmallChess</strong></p>

<p>Other engines adopt other implementations. For example, I prefer a model where I draw a sudden large jump to simulate a gross blunder. The probability depends on the level, and it is related to the number of pieces on the board. When my engine feels it needs to make a blunder, it'd do a quick shallow search. My intention is try to simulate simple tactic errors.</p>
