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
Syntax
random()
Return value
A random integer between -9223372036854775808 and 9223372036854775807.
Examples
SELECT typeof(random()) AS type,
abs(random()) % 6 + 1 BETWEEN 1 AND 6 AS dice_in_range;
Output
type | dice_in_range integer | 1
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
RANDOM()· PostgreSQL
Related
ABS()· Return the absolute value of a number, so abs(-7) is 7. NULL gives NULL and text that isn't a number gives 0.0.- SQL Aggregate Functions · tutorial