Sets Srandmember
# Redis Srandmember Command
[!(#) Redis Sets](#)
The Redis Srandmember command is used to return a random element from a set.
Starting from Redis version 2.6, the Srandmember command accepts an optional count parameter:
* If count is positive and less than the cardinality of the set, the command returns an array containing count distinct elements. If count is greater than or equal to the cardinality of the set, the entire set is returned.
* If count is negative, the command returns an array where elements may be repeated, and the length of the array is equal to the absolute value of count.
This operation is similar to SPOP, except that SPOP removes the randomly selected element from the set and returns it, while Srandmember only returns the random element without modifying the set.
### Syntax
The basic syntax of the redis Srandmember command is as follows:
redis 127.0.0.1:6379> SRANDMEMBER KEY
### Available Versions
>= 1.0.0
### Return Value
When only the key parameter is provided, a single element is returned; if the set is empty, nil is returned. If the count parameter is provided, an array is returned; if the set is empty, an empty array is returned.
### Example
redis 127.0.0.1:6379> SADD myset1 "hello"(integer) 1
redis 127.0.0.1:6379> SADD myset1 "world"(integer) 1
redis 127.0.0.1:6379> SADD myset1 "bar"(integer) 1
redis 127.0.0.1:6379> SRANDMEMBER myset1 "bar"
redis 127.0.0.1:6379> SRANDMEMBER myset1 21) "Hello"2) "world"
[!(#) Redis Sets](#)
AI is thinking...
[](#)[Redis Lists(Li
YouTip