SQL generate a random positive or negative value

Viewed 23

I am looking for a way to change a value randomly from positive to negative. (I am creating a distortion on a lat/long location, so I would like to offset a given location with +/- some degrees)

I already created the following query which give me a number between -1 and +1, the idea is to multiply my distortion with this number to get a random negative or positive number.

SELECT round(-1+3*RAND(),0);

The only problem is, this also generates the value 0.0 which can't be multiplied. How do I get -1 or +1 only?

TIA

ABBOV

1 Answers

Maybe:

  1. start by rounding, to give 0 or 1
  2. then multiply to give 0 or 2
  3. then subtract, to give -1 or 1

i.e.:

SELECT ROUND(RAND()) * 2 - 1;
Related