diff --git a/src/dict.c b/src/dict.c index e197266e..9ba0d7d7 100644 --- a/src/dict.c +++ b/src/dict.c @@ -631,7 +631,11 @@ dictEntry *dictGetRandomKey(dict *d) if (dictIsRehashing(d)) _dictRehashStep(d); if (dictIsRehashing(d)) { do { - h = random() % (d->ht[0].size+d->ht[1].size); + /* We are sure there are no elements in indexes from 0 + * to rehashidx-1 */ + h = d->rehashidx + (random() % (d->ht[0].size + + d->ht[1].size - + d->rehashidx)); he = (h >= d->ht[0].size) ? d->ht[1].table[h - d->ht[0].size] : d->ht[0].table[h]; } while(he == NULL);