How is nitrous oxide formed?

Nitrous Oxide is also the gas that they use at the hospital for surgery to make you fall asleep. And the way that they make it is to lightly heat up ammonium nitrate which will turn to nitrous oxide and water vapor.