We're Moving!

The Vertica Forum is moving to a new OpenText Analytics Database (Vertica) Community.

Join us there to post discussion topics, learn about

product releases, share tips, access the blog, and much more.

Create My New Community Account Now


Generate Random Integers, Including Negative Numbers — Vertica Forum

Generate Random Integers, Including Negative Numbers

Jim Knicely authored this tip.

The RANDOMINT(n) function returns one of the n integers from 0 through n – 1. Those are all positive integers. What if I want to include negative integers? That’s easy with a simple multiplication.

Example:

dbadmin=> SELECT DECODE(randomint(2), 1, 1, -1) * randomint(11) "Random INT from -10 to 10";
Random INT from -10 to 10
---------------------------
                        -1
(1 row)

dbadmin=> SELECT DECODE(randomint(2), 1, 1, -1) * randomint(11) "Random INT from -10 to 10";
Random INT from -10 to 10
---------------------------
                         3
(1 row)

dbadmin=> SELECT DECODE(randomint(2), 1, 1, -1) * randomint(11) "Random INT from -10 to 10";
Random INT from -10 to 10
---------------------------
                        -7
(1 row)

dbadmin=> SELECT DECODE(randomint(2), 1, 1, -1) * randomint(11) "Random INT from -10 to 10";
Random INT from -10 to 10
---------------------------
                         9
(1 row)

Have fun!

Sign In or Register to comment.