MySQL query random bar record SQL statement and PHP compute probabilities

Source: Internet
Author: User
Tags rand
Recently on the Internet to find the next MySQL query random several sql, I put the final record down.
SELECT * from Uchome_mtag as a joins (SELECT MAX (TAGID) as ID from Uchome_mtag) as B
On (A.tagid>=floor (B.id*rand ())) LIMIT 50
I found a problem after the experiment, when the total number of your table and the number of bars you want to get close, it may not be ideal, it is possible that you have 10, you want to find out the random 8, but only give 5 results. It should be the one that is greater than or equal to the cause.

Also have PHP calculate probability, also organize here.

100% if (true)
if (rand (0,1) = = = 0)
1/3 if (rand (0, 2) = = = 0)
1/x if (rand (0, x-1) = = = 0)
X-if (rand (0, < x)
X‰if (rand (0,999) < x)
/**

* Returns true with the probability of $x/$y
*
* @param int $x
* @param int $y
* @return BOOL
*
* Prob = 1/2
* Prob (1,100) = 1/100
*
*/
function Prob ($x, $y) {
Return (Mt_rand (0, $y-1) < $x);
}
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.