24

I would like to implement paging for a random set

Select * from Animals ORDER BY random(SEED) LIMIT 100 OFFSET 50  

I tried to set int to some integer and to some fracture. Doesn't work.

How do I seed random in sqlite?

I known that a similar question already exists, Seeding SQLite RANDOM(), but I don't understand the PHP solution.

15
  • 1
    In your code sample, set SEED to an integer of your choice. For example, Select * from Animals ORDER BY random(9001) LIMIT 100 OFFSET 50 Commented Jun 17, 2014 at 5:29
  • 2
    I tried that. It doesn't work
    – Luda
    Commented Jun 17, 2014 at 7:23
  • 1
    What language are you using?
    – CL.
    Commented Jun 17, 2014 at 7:57
  • possible duplicate of Seeding SQLite RANDOM()
    – chalup
    Commented Jun 17, 2014 at 8:14
  • 1
    @chalup, I have mentioned it in the question. As I mentioned, I didn't get the solution.
    – Luda
    Commented Jun 17, 2014 at 8:44

5 Answers 5

19
+50

Short answer:

You can't. SQLite's random() function does not support a seed value.

Not so short answer:

Checking SQLite's func.c shows that random() is defined without any parameters..

VFUNCTION(random,            0, 0, 0, randomFunc       ),

..and this randomFunc() just calls sqlite3_randomness() (again without any explicit seed value) to obtain a random value of sizeof(sqlite_int64) bytes.

Internally, the implementation of sqlite3_randomness() (see random.c) will set up the RC4 pseudo-random number generator the first time it is used with random seed values obtained from the OS:

  /* Initialize the state of the random number generator once,
  ** the first time this routine is called.  The seed value does
  ** not need to contain a lot of randomness since we are not
  ** trying to do secure encryption or anything like that...
  **
  ** [..]
  */
  if( !wsdPrng.isInit ){
      [..]
      sqlite3OsRandomness(sqlite3_vfs_find(0), 256, k);
      [..]
      wsdPrng.isInit = 1;
  }

Actually, SQLite's unit test functions themselves just use memcpy() on the global sqlite3Prng struct to save or restore the state of the PRNG during test runs.

So, unless you're willing to do something weird (like create a temporary table of consecutive numbers (1..max(Animals)), shuffle those around and use them to select 'random-seeded' RowIds from your Animals table) I suppose you're out of luck.

2
  • 1
    How come? MySQL has this option and sqlite doesn't?
    – Luda
    Commented Jun 30, 2014 at 12:13
  • 5
    Of course, they're completely different products. Both are relational database systems that use the SQL Standard, but that's about it.
    – mvanallen
    Commented Jun 30, 2014 at 13:01
11

I would not usually copy an existing answer, but I can see that you have left a comment asking the author of this answer to explain how it works already a few weeks ago and no explanation has been given. I will therefore copy the relevant part and try to explain whats going on. If this explanation is good, do go and vote on the original answer.

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

The first two rows are just about creating a seed of a sort. The result is a decimal number with lots of decimals like:

0.54534238371923827955579364758491

Then the sql select uses this number to multiply with the numeric row id of every row in the SQLite table. And then the rows are sorted according to the decimal part of the resulting product. Using fewer decimals, the sort order would look something like this:

row id   row id * seed      sort order
1        0.545342384        545342384
2        1.090684767        090684767
3        1.636027151        636027151
4        2.181369535        181369535
5        2.726711919        726711919
6        3.272054302        272054302
7        3.817396686        817396686
8        4.362739070        362739070

After sorting this would be the result:

row id   row id * seed      sort order
2        1.090684767        090684767
4        2.181369535        181369535
6        3.272054302        272054302
8        4.362739070        362739070
1        0.545342384        545342384
3        1.636027151        636027151
5        2.726711919        726711919
7        3.817396686        817396686

In this sample I used only eight rows so the result is not very random looking. With more rows the result will appear more random.

This solution will give you the same order repeatedly as long as:

  • You use the same seed
  • No new rows have appeared in the table and no rows have been deleted from the table
2
  • 1
    Is there a reason you're using a substring rather than " % 1"?
    – keyboardr
    Commented May 9, 2017 at 0:14
  • 1
    That's because sqlite loathes decimals. Using %1 on a decimal value returns 0, and I haven't been able to solve anything with CAST. My workaround is good old (x * 1e7) % 1e7
    – Paul W
    Commented Apr 15, 2018 at 12:44
2

I don't know if you're wanting a PHP and iOS solution, but if you are only interested in iOS and dont care much about using the built-in sqlite random() function, you could declare a custom function to use in your queries, one that does take a seed parameter.

sqlite3_create_function(database, "CUSTOM_RANDOM", 1, SQLITE_UTF8, NULL, &CustomRandomSQLite, NULL, NULL);

.

void CustomRandomSQLite(sqlite3_context* context, int argc, sqlite3_value** argv)
{
    if(argc == 1 && sqlite3_value_type(argv[0]) == SQLITE_INTEGER)
    {
        const int seed = sqlite3_value_int(argv[0]);
        const int result = ...;

        sqlite3_result_int(context, result);
    }
    else
    {
        sqlite3_result_error(context, "Invalid", 0);
    }
}

.

Select * from Animals ORDER BY CUSTOM_RANDOM(SEED) LIMIT 100 OFFSET 50
4
  • What is the ... mean in this row: const int result = ...;
    – Luda
    Commented Jun 30, 2014 at 12:10
  • Where is the seeding taking place?
    – Luda
    Commented Jun 30, 2014 at 12:11
  • ... is a placeholder for your code to take the seed and return a random number into the result variable
    – SomeGuy
    Commented Jun 30, 2014 at 12:24
  • If you just want random numbers, you could also remove the seed and set it up with 0 arguments, then set int result = arc4random()
    – SomeGuy
    Commented Jul 2, 2014 at 11:58
0

To generate Pseudo Random Number Generator (PRNG) per seed, the next PHP function can be used:

function getPrng($seed= NULL){
    if (is_null($seed)) {
      return mt_rand() / mt_getrandmax();
    }
    srand($seed);
    return   mt_rand() / mt_getrandmax();
}

Then use substr of sqlite with rowid as described here

The function generate a decimal number <1 of 14+ digits like 0.19151945001982

if the seed is null, the function generate a different PNRG every time it is called. Using a constant seed, the function generate the same PNRG. The function is executed before query the Sqlite database as given below:

//The PNRG is generated for a seed before executing the query.
$prng = getPrng(1234);
//The query used the rowid of the table
$query = "SELECT * FROM table  ORDER BY substr(rowid * {$prng} , length(rowid) + 2)  limit 3";

You can try the online demo here with sample Sqlite table of 10 rows for demonstration:

sample output:

SQLite version 3.7.17
Selecting 3 random rows of 10, with seed: 0.19151945001982
Id  name    rowid*prng           random
6   p_6     1.1491167001189     14911670011892
10  p_10    1.9151945001982     151945001982
1   p_1     0.19151945001982    19151945001982

-7

I use this for random from seed in my javascript game i am sure you can quite easily convert it to sql

seed: function(max) {
    if(typeof this._random === 'undefined') this._random = max; // init on first run
    this._random = (this._random * 9301 + 49297) % 233280;
    return Math.floor(this._random / (233280.0) * max);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.