Seeding SQLite RANDOM()

mysql, random, seeding, sql, sqlite

Solution

If you need a pseudo-random order, you can do something like this (PHP):

$seed = md5(mt_rand());
$prng = ('0.' . str_replace(['0', 'a', 'b', 'c', 'd', 'e', 'f'], ['7', '3', '1', '5', '9', '8', '4'], $seed )) * 1;
$query = 'SELECT id, name FROM table ORDER BY (substr(id * ' . $prng . ', length(id) + 2))';

Plus, you can set $seed to the predefined value and always get same results.

I've learned this trick from my colleague http://steamcooker.blogspot.com/

Problem

Does SQLite support seeding the `RANDOM()` function the same way MySQL does with `RAND()`? ``` $query = "SELECT * FROM table ORDER BY RAND(" . date('Ymd') . ") LIMIT 1;"; ``` From the MySQL Manual about `RAND(N)`: If a constant integer argument N is specified, it is used as the seed value, which produces a repeatable sequence of column values. In the following example, note that the sequences of values produced by RAND(3) is the same both places where it occurs. If not, is there any way to archive the same effect using only one query?

Original source

Related problems