The Vertica Forum recently got a makeover! Let us know what you think by filling out this short, anonymous survey.

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.

Can't find what you're looking for? Search the Vertica Documentation, Knowledge Base, or Blog for more information.