As far as we can tell, the idea of counting sheep to get to sleep might go back to ancient shepherds, who had to literally count their sheep every night before turning in, to make sure they were all there. Given the timing and the monotony of the task, someone eventually gave it a shot as a sleep aid and it caught on.
But does it work? Not according to researchers at Oxford University. Allison G. Harvey and Suzanna Payne, from the university's Department of Experimental Psychology, conducted a study where volunteer insomniacs were monitored as they tried different distraction techniques for falling asleep over several nights.1
Harvey and Payne found that subjects took longer than usual to fall asleep on the nights they were instructed to count sheep, or were not told to do anything.
When the insomniacs were told to distract themselves with a relaxing scene like a quiet beach, a walk in the woods, a massage, etc., though, they fell asleep 20 minutes sooner, on average, than they did when sheep counting or doing nothing. Harvey and Payne concluded that counting sheep is just too boring to do for very long, but a more engrossing distraction, like the detailed images of relaxing beaches, occupies enough "cognitive space" to keep people from engaging with too many other thoughts or worries.
If you're a sheep counter and it works for you, great! If it doesn't, here are some tips on maintaining good sleep hygiene from the University of Maryland Sleep Disorders Center:
Stick to a fixed bedtime and wake up time. It's easier to fall asleep faster when you're accustomed to sleeping at a certain time.
Avoid alcohol 4-6 hours before bedtime. Alcohol has an immediate sleep-inducing effect, but when the alcohol levels in your blood start to fall a few hours later, there's a stimulating effect, leading to insomnia.
Avoid heavy, spicy, or sugary foods in that same pre-sleep window. These can affect your ability to stay asleep.
Watch your workout time. Exercise in the afternoon can help deepen sleep, but exercise within 2 hours of bedtime can give you trouble when trying to fall asleep.
Find a comfortable temperature. A room that's too cold or too hot can keep you awake. A cool, but not cold, room is the most conducive to sleep for most people.
Don't force it. If you don't fall asleep within 15-30 minutes of getting into your favorite sleeping position, get up, go into another room, and read until you feel sleepy.
That's just the tip of the sleep tips iceberg. What works for you?
1 Harvey, AG and Payne, S. (2002). "The management of unwanted pre-sleep thoughts in insomnia: distraction with imagery versus general distraction." Behaviour Research and Therapy, 40:3 (267-277). DOI:10.1016/S0005-7967(01)00012-2.