title: Multiple rows alternative for RAND()
body: <p>I am having problems with my query for selecting 100 random rows from a database with 50k of rows.</p>
  
  <p>First i only used the function RAND() but that became too slow.</p>
  
  <p>Now i am using this query:</p>
  
  <pre><code>$query = mysql_query("SELECT t1.* FROM users AS t1 JOIN (SELECT ROUND(RAND() * (SELECT MAX(id) FROM users WHERE picture != '' &amp;&amp; age &lt;= 50 &amp;&amp; age &gt;= 18 &amp;&amp; username != '')) AS id) AS t2 WHERE picture != '' &amp;&amp; age &lt;= 50 &amp;&amp; age &gt;= 18 &amp;&amp; username != '' &amp;&amp; t1.id &gt;= t2.id LIMIT 100");
  while($q = mysql_fetch_assoc($query)) 
  { echo $q['id'].'&lt;br&gt;'; }
  </code></pre>
  
  <p>But sometimes it gives a bunch of empty rows.</p>
  
  <p>Can anybody tell me whats going wrong?</p>
  
  <p>Thanks!</p>
  
  <p><strong>UPDATE</strong> I am trying this query too but it gives me an error.</p>
  
  <pre><code>$query = mysql_query("SELECT id FLOOR(1 + RAND() * x.m_id) 'rand_ind' FROM users (SELECT MAX(t.id) - 1 'm_id' FROM users t) x WHERE picture != '' &amp;&amp; age &lt;= 50 &amp;&amp; age &gt;= 18 &amp;&amp; username != '' ORDER BY rand_ind LIMIT 100");
  </code></pre>
  
  <p><strong>mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource</strong></p>