A woman pedals past campaign posters in Nakhon Ratchasima, Thailand, on March 13. (Sakchai Lalit/AP)

NEARLY FIVE years after staging a coup, and after many broken promises to restore democracy, Thailand’s military is at last allowing the semblance of a national election on Sunday. After years of harsh repression, Thais appear eager to vote, and their choice of a new parliament could restore a degree of pluralism in a key Southeast Asian ally of the United States. But if the ultimate result is anything other than the continuation in power of junta leader Prayuth Chan-ocha, it will be because the generals miscalculated just how much to rig the new political system in their favor.

The tilting of what was once a vibrant democracy has been extreme. In addition to changing the election rules to disadvantage the populist movement that has won every election this century, the military gave itself the power to appoint every member of a 250-member Senate, which will join with the 500-seat House of Representatives to choose a prime minister. That means the military’s new political party must win only a quarter of the elected seats to install Mr. Prayuth in the post.

Remarkably, he may yet lose. Despite its years of effort, the regime has failed to eliminate the strength of the movement led by exiled former prime minister Thaksin Shinawatra. The generals deposed Mr. Thaksin in 2006, removed his sister from office in 2014 and imprisoned many supporters, but a party identified with him leads in the pre-election polls. A second Thaksin party was banned this month, and the leaders of another independent movement have been threatened with criminal charges. Yet still the military party could come up short.

It’s not hard to see why. The stern, colorless Mr. Prayuth has harshly suppressed dissent and failed to restore the fast economic growth Thailand once enjoyed. Under his regime, the country has passed India and Russia to become the most economically unequal in the world, with the top 1 percent of Thais controlling 67 percent of wealth. Foreign investment has stagnated, and indebtedness of the country’s farmers has grown. No wonder many in the poor rural north still support Mr. Thaksin’s movement, which while in power showered them with grants and subsidies.

If the election’s result is a weak government, the real winner may be King Maha Vajiralongkorn, who, since inheriting the throne two years ago, has been working to increase his power. An erratic figure who once promoted his poodle, Foo Foo, to the rank of air chief marshal, the king is probably more unpopular even than Mr. Prayuth — but draconian laws against criticizing the royal family shield him.

The United States designated Thailand a major non-NATO ally in 2003, when it was still a democracy. At a time when nearby China is promoting authoritarianism, a return to democracy would help preserve Thailand’s lean toward the United States. Yet the Trump administration has made no effort to check the military’s ma­nipu­la­tion of the election. By now, that passivity has become predictable — which no doubt is one reason the generals have been so blatant in their rigging.