PostgreSQL: Select a Random Number in a Range (Between Two Numbers) - Comment Page: 1

This is quick tip howto select a random number in a range. Example random number between 1-100 (>= 1 and < 100). This is actually very easy job with PostgreSQL own random() function, which returns random value between 0-1. Following example selects 5 random values using generate_series() function: SELECT random() FROM generate_series(1,5); random ------------------- 0.595817462075502 0.350072300527245 0.989073566626757 0.700899163261056 0.940509075298905 (5 rows) Next thing to todo is convert random function values to real float values in a range 1-100: SELECT random() * 99 + 1 FROM generate_series(1,5); ?column? ------------------ ...