Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think an agreeable solution would be to use an asymptotic function (1/x) in combination with weighting.</p> <p>The following has been tested:</p> <pre><code>SELECT *, (Rand()*10 + (1/(max_id - id + 1))) AS weighted_random FROM tbl1 ORDER BY weighted_random DESC LIMIT 4 </code></pre> <p>If you want to get the max_id within the query above, just replace max_id with:</p> <pre><code>(SELECT id FROM tbl1 ORDER BY id DESC LIMIT 1) </code></pre> <p><strong>Examples:</strong></p> <p>Let's say your max_id is 1000 ...</p> <p>For each of several ids I will calculate out the value:</p> <p>1/(1000 - id + 1) , which simplifies out to 1/(1001 - id):</p> <p><em>id: 1000</em></p> <p>1/(1001-1000) = 1/1 = 1</p> <p><em>id: 999</em></p> <p>1/(1001-999) = 1/2 = .5</p> <p><em>id: 998</em></p> <p>1/(1001-998) = 1/3 = .333</p> <p><em>id: 991</em></p> <p>1/(1001-991) = 1/10 = .1</p> <p><em>id: 901</em></p> <p>1/(1001-901) = 1/100 = .01</p> <p>The nature of this 1/x makes it so that only the numbers close to max have any significant weighting. </p> <p>You can see a graph of + more about asymptotic functions here:</p> <p><a href="http://zonalandeducation.com/mmts/functionInstitute/rationalFunctions/oneOverX/oneOverX.html" rel="nofollow">http://zonalandeducation.com/mmts/functionInstitute/rationalFunctions/oneOverX/oneOverX.html</a></p> <p>Note that the right side of the graph with positive numbers is the only part relevant to this specific problem.</p> <p><strong>Manipulating our equation to do different things:</strong></p> <p>(Rand()*a + (1/(b*(max_id - id + 1/b))))</p> <p>I have added two values, "a", and "b"... each one will do different things:</p> <p>The larger "a" gets, the less influence order has on selection. It is important to have a relatively large "a", or pretty much <em>only</em> recent ids will be selected.</p> <p>The larger "b" gets, the more quickly the asymptotic curve will decay to insignificant weighting. If you want more of the recent rows to be weighted, I would suggest experimenting with values of "b" such as: .5, .25, or .1.</p> <p>The 1/b at the end of the equation offsets problems you have with smaller values of b that are less than one.</p> <p><strong>Note:</strong></p> <p>This is not a very efficient solution when you have a large number of ids (just like the other solutions presented so far), since it calculates a value for each separate id.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload