What form of magnesium?
I'll add, the mag oxide always prevents leg cramps when I take a full tablet (250mg) when I am working outside all day (and staying hydrated).
ETA: In my experience magnesium citrate is the one mostly likely to cause diarrhea though they all can.