The exact mechanism by which NDMA forms in ranitidine is not fully understood, but research suggests several possibilities. One hypothesis is that the instability of the ranitidine molecule under heat or acidic conditions can lead to the formation of NDMA. Additionally, improper storage conditions, such as exposure to high temperatures, may exacerbate this process.