← blog  /  tools

Word scramble generator in Python: when LEMON is also MELON

2026-11-11  ·  4 min read  ·  tools

A shuffle can hand back the word itself or another real word, and often leaves a third of it in place. The bigger catch is in the word list: 40% of the short words in 43 of our puzzle-book themes have letters that spell another word.

A word scramble puzzle prints a word with its letters mixed up, and the reader writes the word. The Python is one line: random.shuffle. What that line does not know is whether the result is fair, and a reader notices at once when it is not.

On 30 October 2026 we shuffled the 1,244 words of 43 of our puzzle-book themes, 200 times each, and checked every result against them and a list of 69,973 English words.

The short version#

  • Unchanged: for words of 4 or 5 letters, 2.65% of plain shuffles gave the word back as it was. For 6 or 7 letters, 0.17%.
  • Another real word: 1.50% of short shuffles spelled a different word, the kind of line that looks as if it was never mixed.
  • Barely mixed: in 30.9% of short shuffles more than a third of the letters stayed in their places. For 8 to 12 letters it was still 9.5%.
  • Two right answers: 40.1% of our theme words of 4 or 5 letters have letters that spell another word too: LEMON and MELON, EARTH and HEART. Across all lengths, 287 of 1,244, or 23.1%. No shuffle fixes that; only the choice of words does.

The code#

A word scramble generator with fair mixespython
import random
from collections import defaultdict


def letters_key(word):
    return "".join(sorted(word))


def anagram_index(words):
    """{sorted letters: {words with exactly those letters}}: ACES, CASE and CAFE -> 'ACES': {ACES, CASE}."""
    index = defaultdict(set)
    for w in words:
        index[letters_key(w)].add(w)
    return index


def other_answers(word, index):
    """Other words with exactly the same letters: answers a solver may give that are not yours."""
    return index.get(letters_key(word), set()) - {word}


def scramble(word, real_words, rng=random, most_in_place=1 / 3, tries=1000):
    """Mix the letters until the result is not a real word (the answer included) and at most a third of the letters
    are still where they were."""
    letters = list(word)
    for _ in range(tries):
        rng.shuffle(letters)
        mixed = "".join(letters)
        in_place = sum(a == b for a, b in zip(mixed, word))
        if mixed not in real_words and in_place <= len(word) * most_in_place:
            return mixed
    raise ValueError(f"no fair scramble for {word}")
Run itpython
for w in ("LEMON", "EARTH", "GIRAFFE"):
    print(w, "->", scramble(w, real, random.Random(1)), "| other answers:", sorted(other_answers(w, index)))

That printed LEMON -> MONLE with MELON as another answer, EARTH -> RTHEA with HATER and HEART, and GIRAFFE with none.

A fair mix#

scramble() shuffles again until three things hold: the result is not the answer, it is not any other real word, and at most a third of the letters are where they were. For every one of our theme words of 4 letters or more that took about 5 microseconds a word, so there is no reason to print a weaker mix.

The real-word list matters here. A small list lets more scrambles through that a reader will see as words, so use a big one, plus your own theme words, as we did.

Two right answers#

This is the one that makes readers write in. A reader sees MONLE, writes MELON, and the answer key says LEMON. On a page of fruit both are fair answers, and the reader is right.

other_answers() finds the words with exactly the same letters. The counts depend on how big the word list is: ours includes rare words such as HOERS, so the 23.1% counts some pairs no reader would think of. Short words are the problem, at 40.1%. From 8 letters up it was 5.1%.

There are three ways out: leave those words out, accept every listed answer in the key, or let the theme rule the others out. On a page headed Planets, HEART and HATER are not answers, so EARTH is safe; on a page headed Fruit, LEMON is not. Picking a different word is the simplest.

A whole book, not one page#

Our word scramble book maker makes these checks on every word: a second program confirms that each mixed-up word uses exactly the letters of its answer and that at most a third of them stay in their places, a mixed-up word is never the answer itself or another common English word, and no answer's letters spell another word from the theme lists. Words whose letters also spell another English word are used only when a theme runs short of others, and the theme at the top of the page tells them apart. No word repeats in a book, and the answers are at the back. You pick the level, 103 themes and the trim size, and the book is checked against KDP's paperback specs before you can download it. One word scramble book costs 500 credits, from $3.30 in the big pack.

written