How would you create a function that returns a random number between two number?
example syntax
RandBetween(3,300)
Advertisement
Answer
How about
- Use
RAND()
(which returns a value between 0 and 1 (exclusive). - multiply by 298 (since you want a dynamic range of [300-3] = 297 + 1)
- add 3 to Offset
- and cast to INT?
i.e.
SELECT CAST(RAND() * 298 + 3 AS INT)
(Edit Also see @ivo’s answer for how to turn this into a user defined function)