mirror of
https://codeberg.org/redict/redict.git
synced 2025-01-22 16:18:28 -05:00
Use dictGetFairRandomKey() for HRANDFIELD,SRANDMEMBER,ZRANDMEMBER (#9538)
In the `HRANDFIELD`, `SRANDMEMBER` and `ZRANDMEMBER` commands, There are some strategies that could in some rare cases return an unfair random. these cases are where s small dict happens be be hashed unevenly. Specifically when `count*ZRANDMEMBER_SUB_STRATEGY_MUL > size`, using `dictGetRandomKey` to randomize from a dict will result in an unfair random result.
This commit is contained in:
parent
bdecbd30df
commit
9967a53f4c
@ -1046,7 +1046,7 @@ void hrandfieldWithCountCommand(client *c, long l, int withvalues) {
|
||||
/* Remove random elements to reach the right count. */
|
||||
while (size > count) {
|
||||
dictEntry *de;
|
||||
de = dictGetRandomKey(d);
|
||||
de = dictGetFairRandomKey(d);
|
||||
dictUnlink(d,dictGetKey(de));
|
||||
sdsfree(dictGetKey(de));
|
||||
sdsfree(dictGetVal(de));
|
||||
|
@ -754,7 +754,7 @@ void srandmemberWithCountCommand(client *c) {
|
||||
/* Remove random elements to reach the right count. */
|
||||
while (size > count) {
|
||||
dictEntry *de;
|
||||
de = dictGetRandomKey(d);
|
||||
de = dictGetFairRandomKey(d);
|
||||
dictUnlink(d,dictGetKey(de));
|
||||
sdsfree(dictGetKey(de));
|
||||
dictFreeUnlinkedEntry(d,de);
|
||||
|
@ -4159,7 +4159,7 @@ void zrandmemberWithCountCommand(client *c, long l, int withscores) {
|
||||
/* Remove random elements to reach the right count. */
|
||||
while (size > count) {
|
||||
dictEntry *de;
|
||||
de = dictGetRandomKey(d);
|
||||
de = dictGetFairRandomKey(d);
|
||||
dictUnlink(d,dictGetKey(de));
|
||||
sdsfree(dictGetKey(de));
|
||||
dictFreeUnlinkedEntry(d,de);
|
||||
|
Loading…
Reference in New Issue
Block a user