Redis SPOP不随机的问题

Redissets 结构有一个SPOP方法,方法描述为“Remove and return a random member from a set”,方法说明是随机的。但实际上通过测试表时此方法并不如说的这样随机,比如你将1,2,3,4四个数字用SADD添加到某一个sets里,那么你调用SPOP取出的数据顺序将是一定的。而非随机。

这一问题在Redis的Google Group上被一位Redis的用户提了出来,并怀疑sets里的值是在写数据时就确定了顺序。

Hey Redis Land-

I”m using a normal SET with 1000 members and using SPOP to pop random elements off the set push them onto an in-use set while I use the member. This is in a web app I”m writing. So during development of the app, I have a script that deletes the redis db and repopulates the sets so that I can start fresh again. The weird thing I”ve noticed is that spop docs say that it will return a random member of the set and it seems to do this. but when I recreate the set and casino online start from scratch the elements I start spop”ing are always in the same order. And the first element I SPOP is always the same member after I re-initialize the set.

Does redis seed its random number generator at startup time or do anything so that spop and other “random” commands are truly random and do not repeat the patterns of what elements get returned if you recreate the same set and start over again?



Hello Ezra,

the problem is exactly the one you identified: the PRNG is not seeded
at startup.
The reason this was not changed in the past was to make the system
more predictable, that is, if you get a crash, reproducing it is
simple. But actually it is a bad limit from the point of view of the
distribution of random elements when Redis is restarted.

It”s a one line change, I guess we are just in time to add this for 2.2…

Just pushed on the 2.2 branch and in the unstable branch.






分类 Redis · tag , , , ,