Redis - Set Srandmember Command


Redis SRANDMEMBER command is used to get a random member from set stored at specified key. If called with the additional count argument, return an array of count distinct elements if count 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 numer of returned elements is the absolute value of the specified count.

Return Value

String reply, without the additional count argument. The command returns a Bulk Reply with the randomly selected element, or nil when the key does not exist. Array reply, when the additional count argument is passed the command returns an array of elements, or an empty array when the key does not exist.


Following is the basic syntax of Redis SRANDMEMBER command.

redis> SRANDMEMBER KEY [count] 


redis> SADD myset1 "hello" 
(integer) 1 
redis> SADD myset1 "world" 
(integer) 1 
redis> SADD myset1 "bar" 
(integer) 1 
redis> SRANDMEMBER myset1 
redis> SRANDMEMBER myset1 2 
1) "Hello" 
2) "world"