An ax murderer is attacking you. You find yourself in bed with a coworker. You’re walking down a road and suddenly your feet leave the ground and you’re flying.

Our dreams are the Wild West of our minds where rules and order don’t exist.

Or do they?

The scientists who study dreaming say it isn’t really as strange a phenomenon as it might otherwise seem — and even the really weird dreams probably don’t come out of the blue.

The brain thinks, makes memories, and solves problems. It observes new information. It processes that information by determining what’s important, what’s not, and what’s connected to something you already know. And then the brain either stores that information or dumps what’s not useful, explains Robert Stickgold, PhD, Associate Professor of Psychiatry at Harvard Medical School’s Center for Sleep and Cognition, who studies the role of cognition in sleep.

And you can’t do all of that at the same time, he tells NBC News BETTER. “You can’t both think about something and listen to people at the same time.”

Our brains need offline time for processing and learning new things — and they do this during sleep. (And there’s a whole lot of evidence to support the idea that sleep makes learning and memory storing possible.)

And it might be that dreaming plays a role in that process, Stickgold says — “where the brain is trying to solve problems and complete processes that were going on during waking that it — in its waking hours — didn’t complete.”

Even the really weird dreams may just be part of the brain’s process of elimination-approach to problem solving, experts say. triloks / Getty Images/iStockphoto

The dreaming brain can build stories better than a brain that’s awake

There are certain questions that come up for which we plot a potential course of action or think through a future scenario to solve, Stickgold explains.

That’s what our brains can’t do in the background when we’re awake. But that type of narrative construction (building a story) still requires us to be consciously aware, Stickgold says — which is one feature of dreams. We know they’re happening.

“It might be that you need to bring that sleep-dependent memory processing into consciousness to be able to solve those kinds of problems that require the development of a plan or a narrative or a plot,” Stickgold says — that you need to dream to do that kind of thinking .

Several studies show (what nearly everyone has probably experienced on their own) that our waking experiences show up in our dreams. Other research shows that we are more likely to remember something if we dream about it. (Erin Wamsley, PhD, an assistant professor in the Psychology Department at Furman University in Greenville, South Carolina, explains all of those studies in further depth in a review article in a 2014 issue of Current Neurology and Neuroscience Reports.)

And more research suggests that dreaming does actually help us problem solve.

A set of experiments conducted by Wamsley’s and Stickgold’s group (when Wamsley was at Harvard) showed that when a group of 99 individuals were given the task of navigating a complex maze, those whose performance on the task improved the most when re-tested after a five-hour period were those individuals given the opportunity to take a nap — and more specifically those who reported dreaming about the maze during that nap — compared to when they were simply awake during that time (even if they reported thinking about the maze during that period of being awake).

And perhaps more interesting still, research that looks at the mechanical changes in the brain during sleep and during dreaming align with this thinking, too.

During dreaming, the visual and emotional processing areas of the brain are active

Researchers have measured brain activity during sleep and during dreaming. “And it does match the psychology in some interesting ways,” says Benjamin Baird, PhD, a researcher at the Center for Sleep and Consciousness at University of Wisconsin-Madison, whose work focuses on understanding the neural mechanisms of consciousness.

Studies going back to the 1990s suggest the amygdala (a part of our brain that plays a role in emotional processing) appear to be very active during dreaming. And more recent work from Baird’s group suggests that areas of the brain known to be involved in visual processing (the regions that appear to allow us to register colors, motion, and faces) are active during dreaming.

On the flip side, other parts of the brain (the frontal and prefrontal cortices, which are involved with our ability to plan, think through things and apply logic and order) are less active during dreaming compared with other parts of sleep and wakefulness (that research also goes back to the late 1990s). “That doesn’t mean they’re totally off, but it means activity is suppressed to a very large extent,” Baird says.

All of that seems to fit our understanding of dreaming, Baird says — that we are visually and emotionally processing things, but certain executive processing functions (like being able to plan out the future or weigh the past against a present scenario) don’t really happen during dreaming.

The big caveat, however, is that nearly all of those studies have come to those conclusions using rapid eye movement (REM) sleep to measure dream sleep, Baird says — “which is a big (and actually a wrongful) assumption.”

We dream in the non-REM stages of sleep as well as in the REM stages of sleep, and we spend some of our time in REM sleep not dreaming.

But, Baird adds, REM sleep can be useful for approximating what’s happening during dreaming because it’s during REM sleep that we tend to have the most vivid, story-like dreams. And data suggests that we spend the vast majority of our time in REM sleep (as much as 95 percent of it, according to research from Baird and his colleagues published in 2017 in the journal Nature Neuroscience) dreaming.

“So looking at the brain during REM sleep can still give us some clues about what’s happening,” Baird says.

And it’s not actually that difficult to understand that when this type of brain activity happens, we dream, he adds.

“What the brain is doing at all times is trying to construct a model of the world around us from the best input it has,” Baird says. When we’re awake the input comes from our environment (what we see, taste, smell, hear, and feel). But when we’re asleep, it may be trying to do the same thing — but the input comes from within.

“During sleep — and particularly during REM sleep when the brain is becoming activated again — the brain tries to do what it always does: it tries to construct a reasonable model of the world,” he says.

So, why do we dream the truly wacky dreams?

But some dreams are very far from reasonable. Some dreams are really weird.

Even the really weird dreams may just be part of the brain’s process of elimination-approach to problem solving, according to Stickgold.

A lot of memory processing happens during sleep, he says. The brain is filing away new memories, deciding which ones to store and which ones not to. If we presume the brain is indeed problem solving during dreaming, it’s going to look through all of those files (sometimes the ones you just put there and sometimes the really old, dusty ones from a while back) and try to find something useful.

“Your brain is looking for associated memories that are relevant to recent events,” Stickgold explains.

And remember the emotional centers of the brain are more active during REM, and the parts responsible for logical thinking are less active. So it would make sense that the memories the brain uses during dreaming may be the more emotionally charged ones, and ones that may not fit the logical narrative.

This is when your brain is trying the out-of-the-box solutions, Stickgold says. It’s not necessarily looking for something reliable that works every time.

“The brain is acting like a venture capitalist,” he says. It’s intentionally throwing a lot of spaghetti against the wall to see what sticks, knowing that some of it won’t.

MORE "THIS IS YOUR BRAIN ON"

Want more tips like these? NBC News BETTER is obsessed with finding easier, healthier and smarter ways to live. Sign up for our newsletter.