Generate Random Ints with RANDOMINT

Moderator: NorbertKrupa

Post Reply
User avatar
JimKnicely
Site Admin
Site Admin
Posts: 1825
Joined: Sat Jan 21, 2012 4:58 am
Contact:

Generate Random Ints with RANDOMINT

Post by JimKnicely » Fri Dec 21, 2012 5:31 pm

Hi,

It’s very easy to generate random integers in Vertica using the RANDOMINT function!

This RANDOMINT function returns a uniformly-distributed integer I, where 0 <= I < N, where N <= MAX_INT8. That is, RANDOMINT(N) returns one of the N integers from 0 through N-1.

The general syntax of the function is:
  • RANDOMINT (N)
Example:

To generate several random integers between 0 and 5, inclusive, we could run a query like this one:

Code: Select all

dbadmin=> SELECT RANDOMINT(6) random_1,
dbadmin->        RANDOMINT(6) random_2,
dbadmin->        RANDOMINT(6) random_3,
dbadmin->        RANDOMINT(6) random_4;
random_1  | random_2 | random_3 | random_4
----------+----------+----------+----------
        5 |        1 |        0 |        4
(1 row)
Have fun!
Jim Knicely

Image

Note: I work for Vertica. My views, opinions, and thoughts expressed here do not represent those of my employer.

User avatar
piglet
Beginner
Beginner
Posts: 45
Joined: Tue Feb 07, 2012 4:04 pm

Re: Generate Random Ints with RANDOMINT

Post by piglet » Sat Dec 22, 2012 11:31 am

Hmm.

Why did Vertica choose that the value we send into the RANDOMINT function parameter not be included as one of the available return values? I would hope that RANDINT(10) could return a random int between 0 and 10 and not 0 and < 10.

Post Reply

Return to “Vertica Tips, Lessons and Examples”