Long ago, Sesame Street introduced my preschool self to the notion of counting sheep. This was the late-eighties, so my imagination was still drawn in clip-art: cottony cartoon sheep jumping one at a time over a single section of fence. Of course, I was an easily distractible kid. I never managed to count more than 3 sheep at a time before my thoughts drifted elsewhere.

Over time, I began to wonder if I was doing it wrong. Some later storybooks depicted the sheep sailing over the moon or leaping over the bed. Maybe the fence was the problem. In my teens, I had this epipheny: Maybe instead of ticking off individual sheep jumps, I was supposed to be tallying a flock in a field. Of course, that turned out to be an even more insomnia-inducing task which demanded great concentration and frustration. How was this helpful? Where did this silly practice come from anyway? And why sheep and not horses or kites or anything else?

History’s first sheep counters were obviously shepherds, who, while keeping watch over their flocks, maintained a careful head count at the end of each day. While not the only mundane job task in the ancient world, this one would certainly become associated directly with bedtime. However, that’s not the whole of the story: the practice of counting sheep still had to move beyond the hillsides and into the cultural landscape.

As early as the 12th century, a book of myths drawing largely from Islamic culture made reference to counting sheep as a means to induce boredom. According to a story from the Disciplina Clericalis, a certain king liked to hear stories before bed. One night, when his storyteller wanted to catch some Z’s himself, he decided to shortcut story hour by telling a repetitive tale.

In his story, a farmer bought 2000 sheep at a market. To get them home across the river, the farmer had to ferry them in his boat two at a time. The clever storyteller described the first trip across the river and then nodded off. Naturally, the King woke him and demanded to hear the rest of the story. “Simple,” the storyteller yawned. “The same thing happens 999 more times.” With that, he went back to sleep and left the King to imagine the rest of the story.

The notion of counting sheep to induce thoughtlessness continued as a cultural trope for the next centuries. In 1605, Cervantes’ “Don Quixote” had Sancho Panza telling the same story to the Don (although this time with goats) in order to occupy his mind and keep him from rushing with quixotic chivalry towards some ominous sound.

The story may have started with sheep crossing rivers two-by-two, but at some point they reached land and found their next boring hurdle: jumping fences.  The first written reference in 1832 described how a landowner was lulled to sleep by the monotony of beholding his sheep clumsily stepping through a breach in a fence.

For at least a millennium, counting sheep to fall asleep has been a widely recognized practice. Despite this, our own experience and even cognitive research has shown that the attention it demands might actually discourage sleep. A study in the early 2000s divided insomniacs into three groups. The group that was asked to picture calming environments fell asleep before those who were left alone with their thoughts. However, the group that was asked to count sheep took much longer to fall asleep.

For this reason, experts agree that picturing tranquil images is the way to go. Now, if sheep grazing on an English hillside is your idea of a serene environment, by all means go ahead. Just don’t try to keep track!