James David Lewis-Williams said this quote

Some researchers believe that dreaming is what happens when sensory input to the brain is greatly diminished: the brain then ‘freewheels’, synapses firing more or less at random, and the brain tries to make sense of the resultant stream of images.