content/commands/hrandfield.md
When called with just the key argument, return a random field from the hash value stored at key.
If the provided count argument is positive, return an array of distinct fields.
The array's length is either count or the hash's number of fields ([HLEN]({{< relref "/commands/hlen" >}})), whichever is lower.
If called with a negative count, the behavior changes and the command is allowed to return the same field multiple times.
In this case, the number of returned fields is the absolute value of the specified count.
The optional WITHVALUES modifier changes the reply so it includes the respective values of the randomly selected hash fields.
{{% redis-cli %}} HSET coin heads obverse tails reverse edge null HRANDFIELD coin HRANDFIELD coin HRANDFIELD coin -5 WITHVALUES {{% /redis-cli %}}
When the count argument is a positive value this command behaves as follows:
count is bigger than the number of fields in the hash, the command will only return the whole hash without additional fields.When the count is a negative value, the behavior changes as follows:
count fields, or an empty array if the hash is empty (non-existing key), are always returned.| Redis Software | Redis Cloud | <span style="min-width: 9em; display: table-cell">Notes</span> | |:----------------------|:-----------------|:------| | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | <span title="Supported">✅ Standard</span> <span title="Supported"><nobr>✅ Active-Active</nobr></span> | |
{{< multitabs id="hrandfield-return-info" tab1="RESP2" tab2="RESP3" >}}
Any of the following:
count option is not usedcount fields when the count option is used, or an empty array if the key does not exists.count and WITHVALUES were both used.-tab-sep-
Any of the following:
count option is not usedcount fields when the count option is used, or an empty array if the key does not exists.count and WITHVALUES were both used.{{< /multitabs >}}