zrandmember.3valkey - Man Page

Returns one or more random members from a sorted set.

Synopsis

ZRANDMEMBER key [count [WITHSCORES]]

Description

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

If the provided count argument is positive, return an array of distinct elements. The array’s length is either count or the sorted set’s cardinality (ZCARD), whichever is lower.

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.

The optional WITHSCORES modifier changes the reply so it includes the respective scores of the randomly selected elements from the sorted set.

Reply

Resp2

valkey-protocol(7) Bulk string reply: without the additional count argument, the command returns a randomly selected member, or valkey-protocol(7) Nil reply when key doesn’t exist.

valkey-protocol(7) Array reply: when the additional count argument is passed, the command returns an array of members, or an empty array when key doesn’t exist. If the WITHSCORES modifier is used, the reply is a list of members and their scores from the sorted set.

Resp3

valkey-protocol(7) Bulk string reply: without the additional count argument, the command returns a randomly selected member, or valkey-protocol(7) Null reply when key doesn’t exist.

valkey-protocol(7) Array reply: when the additional count argument is passed, the command returns an array of members, or an empty array when key doesn’t exist. If the WITHSCORES modifier is used, the reply is a list of members and their scores from the sorted set.

Complexity

O(N) where N is the number of members returned

Acl Categories

@read @slow @sortedset

History

Examples

127.0.0.1:6379> ZADD dadi 1 uno 2 due 3 tre 4 quattro 5 cinque 6 sei
(integer) 6
127.0.0.1:6379> ZRANDMEMBER dadi
"uno"
127.0.0.1:6379> ZRANDMEMBER dadi
"uno"
127.0.0.1:6379> ZRANDMEMBER dadi -5 WITHSCORES
 1) "cinque"
 2) "5"
 3) "sei"
 4) "6"
 5) "quattro"
 6) "4"
 7) "quattro"
 8) "4"
 9) "sei"
10) "6"

Specification of the behavior when count is passed

When the count argument is a positive value this command behaves as follows:

  • No repeated elements are returned.
  • If count is bigger than the cardinality of the sorted set, the command will only return the whole sorted set without additional elements.
  • The order of elements in the reply is not truly random, so it is up to the client to shuffle them if needed.

When the count is a negative value, the behavior changes as follows:

  • Repeating elements are possible.
  • Exactly count elements, or an empty array if the sorted set is empty (non-existing key), are always returned.
  • The order of elements in the reply is truly random.

See Also

bzmpop(3valkey), bzpopmax(3valkey), bzpopmin(3valkey), zadd(3valkey), zcard(3valkey), zcount(3valkey), zdiff(3valkey), zdiffstore(3valkey), zincrby(3valkey), zinter(3valkey), zintercard(3valkey), zinterstore(3valkey), zlexcount(3valkey), zmpop(3valkey), zmscore(3valkey), zpopmax(3valkey), zpopmin(3valkey), zrange(3valkey), zrangestore(3valkey), zrank(3valkey), zrem(3valkey), zremrangebylex(3valkey), zremrangebyrank(3valkey), zremrangebyscore(3valkey), zrevrank(3valkey), zscan(3valkey), zscore(3valkey), zunion(3valkey), zunionstore(3valkey)

Info

2024-09-23 8.0.0 Valkey Command Manual