问题
Using MySQL 5.6.21 on Windows 7.
I am attempting to return a 'random' row from a table seeded by the date (so the same row returns for the current day and then switches the next day etc - a "random quote of the day generator" if you like).
I noticed the same rows keep coming up so I simplified the query to its basics, it appears the RAND() function generates very similar numbers every fourth seed value. When rounded to an integer the values appear to repeat every fourth seed. This example only uses 16 rows, but you get the idea.
create table t (i INT);
insert into t values(0),(1),(2),(3),(4),(5),(6),(7),(8),(9),(10),(11),(12),(13),(14),(15);
select i, ceil(rand(i) * 16), rand(i) from t;
drop table t;
Gives...
0 3 0.15522042769493574
1 7 0.40540353712197724
2 11 0.6555866465490187
3 15 0.9057697559760601
4 3 0.15595286540310166
5 7 0.40613597483014313
6 11 0.6563190842571847
7 15 0.9065021936842261
8 3 0.15668530311126755
9 7 0.406868412538309
10 11 0.6570515219653505
11 15 0.907234631392392
12 3 0.15741774081943347
13 7 0.40760085024647497
14 11 0.6577839596735164
15 15 0.9079670691005579
Not what I expected, so what am I doing wrong? I expected a pseudo-random sequence to be generated.
回答1:
According to documentation RAND(n) is working properly only if n is constant. The effect of using a nonconstant argument is undefined. As of MySQL 5.0.13, nonconstant arguments are not permitted.
And as they say RAND() is not meant to be a perfect random generator. It is a fast way to generate random numbers on demand that is portable between platforms for the same MySQL version.
MySQL documentation
Check what will happen if you use RAND() without parameter.
来源:https://stackoverflow.com/questions/27187207/mysql-rand-seed-values-almost-repeat