{"id":8637,"date":"2020-05-19T10:13:22","date_gmt":"2020-05-19T00:13:22","guid":{"rendered":"https:\/\/database.guide\/?p=8637"},"modified":"2020-06-28T09:40:13","modified_gmt":"2020-06-27T23:40:13","slug":"how-sqlite-random-works","status":"publish","type":"post","link":"https:\/\/database.guide\/how-sqlite-random-works\/","title":{"rendered":"How SQLite Random() Works"},"content":{"rendered":"\n<p>The <a href=\"https:\/\/database.guide\/what-is-sqlite\/\">SQLite<\/a> <code>random()<\/code> function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807.<\/p>\n\n\n\n<p>A pseudo-random number is a number that appears to be random, but is not truely random. A pseudo-random number is not truely random because its value was generated by a known seed. However, a pseudo-random number will appear to be random if the user has no knowledge of the seed or algorithm that created it.<\/p>\n\n\n\n<p>Therefore, pseudo-random numbers are often considered good enough for many applications.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p>Here&#8217;s an example of generating a pseudo-random number with the <code>random()<\/code> function.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT random();<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">-882536775989953141<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Positive Values Only<\/h2>\n\n\n\n<p>You can combine <code>random()<\/code> with <code>abs()<\/code> to generate only positive values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT abs(random());<\/code><\/pre>\n\n\n\n<p>In this case, if <code>random()<\/code> generates a negative value, <code>abs()<\/code> will return the absolute value instead.<\/p>\n\n\n\n<p>To demonstrate how <code>abs()<\/code> works, here&#8217;s what happens if I pass the (negative) random number from the first example to the <code>abs()<\/code> function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT abs(-882536775989953141);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">882536775989953141<\/pre>\n\n\n\n<p>So if we pass <code>random()<\/code> to <code>abs()<\/code>, and <code>random()<\/code> generates a negative value, <code>abs()<\/code> will return that value as a positive value.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Value Between 0 and 100<\/h2>\n\n\n\n<p>Here&#8217;s an example of generating a positive number between 0 and 100.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT abs(random() % 100);<\/code><\/pre>\n\n\n\n<p>Here&#8217;s an example of selecting multiple random values.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n  abs(random() % 100) AS R1,\n  abs(random() % 100) AS R2,\n  abs(random() % 100) AS R3;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">R1&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R2&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; R3 &nbsp; &nbsp; &nbsp; &nbsp;\n----------&nbsp; ----------&nbsp; ----------\n17&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 79&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 90 &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Return Random Rows<\/h2>\n\n\n\n<p>You can use <code>random()<\/code> in an <code>ORDER BY<\/code> clause of a database query to return random rows.<\/p>\n\n\n\n<p>Here&#8217;s an example.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM Artist \nORDER BY random() LIMIT 5;<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ArtistId&nbsp; &nbsp; Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n----------&nbsp; ----------------------------------------\n131 &nbsp; &nbsp; &nbsp; &nbsp; Smashing Pumpkins&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n127 &nbsp; &nbsp; &nbsp; &nbsp; Red Hot Chili Peppers&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n169 &nbsp; &nbsp; &nbsp; &nbsp; Black Eyed Peas&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n60&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Santana Feat. Dave Matthews&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n240 &nbsp; &nbsp; &nbsp; &nbsp; Gustav Mahler&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<p>And here&#8217;s what I get if I run it again:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ArtistId&nbsp; &nbsp; Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n----------&nbsp; ----------------------------------------\n234 &nbsp; &nbsp; &nbsp; &nbsp; Orchestra of The Age of Enlightenment&nbsp; &nbsp;\n126 &nbsp; &nbsp; &nbsp; &nbsp; Raul Seixas&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n76&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Creedence Clearwater Revival &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n182 &nbsp; &nbsp; &nbsp; &nbsp; Nega Gizza &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n63&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Santana Feat. Lauryn Hill &amp; Cee-Lo &nbsp; &nbsp; &nbsp;<\/pre>\n\n\n\n<p>If you have a large table, you might want to modify your query to something like this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT * FROM Artist \nWHERE ArtistId IN \n  (SELECT ArtistId FROM Artist ORDER BY random() LIMIT 5);<\/code><\/pre>\n\n\n\n<p>Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">ArtistId&nbsp; &nbsp; Name &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n----------&nbsp; ----------------------------------------\n45&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Sandra De S\u00e1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n105 &nbsp; &nbsp; &nbsp; &nbsp; Men At Work&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n110 &nbsp; &nbsp; &nbsp; &nbsp; Nirvana&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n205 &nbsp; &nbsp; &nbsp; &nbsp; Chris Cornell&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;\n267 &nbsp; &nbsp; &nbsp; &nbsp; G\u00f6teborgs Symfoniker &amp; Neeme J\u00e4rvi&nbsp; &nbsp; &nbsp;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>The SQLite random() function returns a pseudo-random integer between -9223372036854775808 and +9223372036854775807. A pseudo-random number is a number that appears to be random, but is not truely random. A pseudo-random number is not truely random because its value was generated by a known seed. However, a pseudo-random number will appear to be random if the &#8230; <a title=\"How SQLite Random() Works\" class=\"read-more\" href=\"https:\/\/database.guide\/how-sqlite-random-works\/\" aria-label=\"Read more about How SQLite Random() Works\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[100],"tags":[93],"class_list":["post-8637","post","type-post","status-publish","format-standard","hentry","category-sqlite","tag-functions"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8637","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=8637"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8637\/revisions"}],"predecessor-version":[{"id":9345,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/8637\/revisions\/9345"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=8637"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=8637"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=8637"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}