Pre-cum is the male equivalent of vaginal lubrication, so it is a response to being aroused rather than specifically from masturbating so, while it can appear when masturbating, it can also appear when leaving it alone.
If anything, masturbating when you have the privacy to do so could make you less horny when your are in public. Other than that, just try to keep your mind off sexual things when you’re in public and supposed to be doing something else.