srandmember
SRANDMEMBER
Apply for Redis Developer Course
|
Redis Regular Inspection / Technical Support
|
Redis Enterprise Server
|
|---|
Get a random member from a set
Usage is srandmember key.
SPOP retrieves and deletes the data; this command only retrieves it.
Example
| Command> | sadd myset A B C D |
| Result> | 4 |
| Command> | srandmember myset |
| Result> | B |
| Command> | srandmember myset |
| Result> | C |
count is available since version 2.6
Usage is srandmember key count.
If count is positive, members are returned without duplicates.
If count is negative, members may be returned with duplicates.
This is like drawing balls from a bag.
For example, when count is 2, two balls are drawn; if A was drawn first, the next one is drawn from B, C, D.
When count is -2, two balls are drawn; if A was drawn first, A is put back in the bag and the next one is drawn from A, B, C, D.
Therefore A,A can be returned.
Example
| Command> | srandmember myset 2 |
| Result> | 0) A 1) B |
| Command> | srandmember myset -2 |
| Result> | 0) A 1) A |
When count is greater than the number of members
If count is positive, it returns as many members as exist.
If count is negative, it returns count members, allowing duplicates.
Example
| Command> | srandmember myset 6 |
| Result> | 0) A 1) B 2) C 3) D |
| Command> | srandmember myset -6 |
| Result> | 0) A 1) B 2) C 3) D 4) B 5) C |
Syntax
SRANDMEMBER key [count]
- Available since version 1.0.0.
- Time complexity is O(N). N is the count.
- count is available since version 2.6.
| Related commands | SPOP |
| Clients for Java | Jedis, Lettuce, Redisson | Clients for C | Hiredis |
| << SPOP | SRANDMEMBER | SSCAN >> |
|---|
