# SRANDMEMBER

> Return one or more random members of a set without removing them.

URL: https://docs.hotkv.com/commands/srandmember/

```
SRANDMEMBER key [count]
```

- Group: Set
- Complexity: O(1) without count, otherwise O(N) where N is the absolute value of count
- HotKV Cloud: all plans
- On-Premise: no license needed

`SRANDMEMBER` returns random members of the set stored at `key`, leaving the
set unchanged.

- Without `count`, it returns one random member.
- With a positive `count`, it returns up to `count` distinct members. If the
  set has `count` members or fewer, you get all of them.
- With a negative `count`, it returns exactly `|count|` members, and the same
  member can appear more than once.
- A `count` of `0` returns an empty array.

## Return value

| Case | RESP2 | RESP3 |
| --- | --- | --- |
| No `count` | Bulk string: a member, or nil bulk string if the key does not exist | Bulk string, or null |
| With `count` | Array of members (empty if the key does not exist) | Array of members |

With `count` the reply is always an array, also under RESP3, because a
negative count can repeat members.

Errors:

- `ERR value is not an integer or out of range` if `count` is not an integer.
- `ERR value is out of range, value must between -9223372036854775807 and 9223372036854775807`
  for a `count` of -9223372036854775808.
- `ERR syntax error` if more than one `count` argument is given.
- `ERR count is too large, the reply would exceed 512 MB` if a negative
  `count` would build a reply larger than 512 MB.
- `WRONGTYPE` if `key` holds a value that is not a set.

## Examples

```
127.0.0.1:6379> SADD colors "red" "green" "blue"
(integer) 3
127.0.0.1:6379> SRANDMEMBER colors
"green"
127.0.0.1:6379> SRANDMEMBER colors 5
1) "blue"
2) "red"
3) "green"
127.0.0.1:6379> SRANDMEMBER colors -4
1) "red"
2) "red"
3) "blue"
4) "green"
```

The members you get back are random, so your output will differ.

## Compatibility

Same as Redis 7.4 and Valkey 8.

## See also

[SPOP](https://docs.hotkv.com/commands/spop/index.md), [SMEMBERS](https://docs.hotkv.com/commands/smembers/index.md), [HRANDFIELD](https://docs.hotkv.com/commands/hrandfield/index.md),
[ZRANDMEMBER](https://docs.hotkv.com/commands/zrandmember/index.md)