SQLite 3 reference · Math Functions

SQLite RANDOM() Function

Return a random 64-bit signed integer. Use abs(random()) % n or ORDER BY random() for random choices.

SQLite 3

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

random()

Return value

A random integer between -9223372036854775808 and 9223372036854775807.

Examples

A random dice rollTry it yourself
SELECT typeof(random()) AS type,
       abs(random()) % 6 + 1 BETWEEN 1 AND 6 AS dice_in_range;

Output

type | dice_in_range
integer | 1
Pick random rowsTry it yourself
SELECT count(*) AS picked
FROM (SELECT name FROM products ORDER BY random() LIMIT 3);

Output

picked
3

ORDER BY random() LIMIT n sorts the whole table, which is fine for small tables but slow on large ones. It isn't suitable for anything security-related.

In other languages

Was this page helpful?