Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

sql
mysql
rand
communitycreator

What is RAND() in SQL?

Harris Amjad

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

The RAND() function returns a random float value greater than or equal to 0 and less than 10 <= value < 1.

Figure 1 shows a visual representation of the RAND() function.

Figure 1: Visual representation of RAND() function

Syntax

RAND(value)

Parameter

The RAND() function takes a value as a parameter, which acts as a seed value.

This is an optional parameter.

Return value

The RAND() function returns a random float value greater than or equal to 0 and less than 1.

If the value of the optional parameter is specified, then this function returns a repeatable sequence of random numbers.

The same seed values will produce the same random number.

Code

-- without seed value
SELECT RAND();
SELECT RAND();
-- with seed value and it shows that two rand() functions
-- with same seed value produce the same random number
SELECT RAND(10);
SELECT RAND(10);
-- rand() used with floor() to produce random integer
-- 10<=number<=20
SELECT FLOOR(10 + RAND()*(20-10+1))

RELATED TAGS

sql
mysql
rand
communitycreator

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring