SRANDMEMBER key [count]

    Time complexity: Without the count argument O(1), otherwise O(N) where N is the absolute value of the passed count.

    When called with just the argument, return a random element from the set value stored at key.

    Starting from Redis version 2.6, when called with the additional count argument, return an array of count distinct elements if is positive. If called with a negative count the behavior changes and the command is allowed to return the same element multiple times. In this case the number of returned elements is the absolute value of the specified count.

    When called with just the key argument, the operation is similar to , however while SPOP also removes the randomly selected element from the set, will just return a random element without altering the original set in any way.

    redis> SADD myset one two three

    redis> SRANDMEMBER myset

    redis> SRANDMEMBER myset 2

    redis> SRANDMEMBER myset -5

    redis>
    • No repeated elements are returned.

    When instead the count is negative, the behavior changes and the extraction happens as if you put the extracted element inside the bag again after every extraction, so repeated elements are possible, and the number of elements requested is always returned as we can repeat the same elements again and again, with the exception of an empty Set (non existing key) that will always produce an empty array as a result.

    The distribution of the returned elements is far from perfect when the number of elements in the set is small, this is due to the fact that we used an approximated random element function that does not really guarantees good distribution.

    The algorithm used, that is implemented inside dict.c, samples the hash table buckets to find a non-empty one. Once a non empty bucket is found, since we use chaining in our hash table implementation, the number of elements inside the bucket is checked and a random element is selected.

    This means that if you have two non-empty buckets in the entire hash table, and one has three elements while one has just one, the element that is alone in its bucket will be returned with much higher probability.