SRANDMEMBER(3valkey)

πŸ“¦
version 9.0.6 Β· 2 versions
, in fedora 44

✨ SRANDMEMBER β€” ok bestie let's grab some random members πŸ’–

So this is a page from the Valkey Command Manual, and it's all about srandmember, the command that reaches into a set and yanks out random element(s) like it's pulling names out of a hat. πŸŽ€ Manual name's SRANDMEMBER(3valkey) btw, in case you needed the receipts.


🌸 NAME 🌸

srandmember β€” get one or multiple random members from a set. That's it, that's the whole job. πŸ’…


πŸ¦‹ SYNOPSIS πŸ¦‹

SRANDMEMBER key [count]

✨ DESCRIPTION ✨

Okay so when you call it with just the key, you get back a random element from the set value stored at that key. One element. Random. That's the vibe.

But if you pass a positive count? Now we're talking. It returns an array of distinct elements β€” no dupes, none, zero. And the array's length is either count or the set's cardinality (SCARD), whichever one is lower. So if you ask for more than the set actually has, it just gives you what it's got. πŸ™ˆ

Now if the count is negative, bestie, the rules change entirely: the command is allowed to return the same element multiple times. And the number of returned elements is the absolute value of the count you specified. So -5 means five elements, and yes, they can absolutely be the same one over and over. πŸ’€


πŸ’œ REPLY πŸ’œ

RESP2

One of the following:

  • valkey-protocol(7) Bulk string reply: without the additional count argument, the command returns a randomly selected member, or a valkey-protocol(7) Nil reply when key doesn't exist.
  • valkey-protocol(7) Array reply: when the optional count argument is passed, the command returns an array of members, or an empty array when key doesn't exist.

RESP3

One of the following:

  • valkey-protocol(7) Bulk string reply: without the additional count argument, the command returns a randomly selected member, or a valkey-protocol(7) Null reply when key doesn't exist.
  • valkey-protocol(7) Array reply: when the optional count argument is passed, the command returns an array of members, or an empty array when key doesn't exist.
Not the Nil vs Null glow-up between RESP2 and RESP3 btw β€” same energy, different name. πŸ‘‘

πŸ”₯ COMPLEXITY πŸ”₯

Without the count argument it's O(1) β€” instantly iconic. Otherwise it's O(N), where N is the absolute value of the passed count. So yeah, big negative numbers still cost you. ⭐


πŸ‘‘ ACL CATEGORIES πŸ‘‘

@read @set @slow


🌈 HISTORY 🌈

  • Available since: 1.0.0
  • Changed in 2.6.0: Added the optional count argument.

πŸ“ EXAMPLES πŸ“

127.0.0.1:6379> SADD myset one two three
(integer) 3
127.0.0.1:6379> SRANDMEMBER myset
"three"
127.0.0.1:6379> SRANDMEMBER myset 2
1) "one"
2) "three"
127.0.0.1:6379> SRANDMEMBER myset -5
1) "two"
2) "one"
3) "one"
4) "one"
5) "two"

🧚 Specification of the behavior when count is passed 🧚

When count is a positive value, it behaves like this:

  • No repeated elements are returned. Zero. None.
  • If count is bigger than the set's cardinality, the command will only return the whole set without additional elements.
  • The order of elements in the reply is not truly random, so it's 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 set is empty (non-existing key), are always returned.
  • The order of elements in the reply is truly random. slay. ✨

πŸ’– SEE ALSO πŸ’–

sadd(3valkey), scard(3valkey), sdiff(3valkey), sdiffstore(3valkey), sinter(3valkey), sintercard(3valkey), sinterstore(3valkey), sismember(3valkey), smembers(3valkey), smismember(3valkey), smove(3valkey), spop(3valkey), srem(3valkey), sscan(3valkey), sunion(3valkey), sunionstore(3valkey)


And that's the whole page, bestie!! 🫢 This one's from version 9.0.0, dated 2025-10-21, and it's a SRANDMEMBER(3valkey) situation through and through. Go grab yourself a random element, you've earned it. πŸ’…βœ¨

πŸ“¦ where this page comes from