Recently, I found several random SQL queries in mysql on the Internet. I recorded the results. SELECT * FROMuchome_mtagASaJOIN (SELECTMAX (tagid) ASi
Recently, I found several random SQL queries in mysql on the Internet. I recorded the results. SELECT * FROM uchome_mtag AS a JOIN (select max (tagid) AS I
Recently, I found several random SQL queries in mysql on the Internet. I recorded the results.
SELECT * FROM uchome_mtag AS a JOIN (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 in your table is very close to the number of items you want, it may be unsatisfactory. Maybe you have 10, when you want to find 8 random results, only 5 results are given. It should be the one that is greater than or equal to the cause.
There is also a php probability calculation. Let's sort it out here.
100% if (true)
1/2 if (rand (0, 1) = 0)
1/3 if (rand (0, 2) = 0)
1/x if (rand (0, x-1) = 0)
X % if (rand (0, 99) <x)
X ‰ if (rand (0,999) <x)
/**
* Returns true based on 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 );
}