It returns one or more random elements from a set stored at a given key without removing them.
Click to reveal answer
beginner
How do you get exactly 3 random elements from a Redis set using SRANDMEMBER?
Use SRANDMEMBER key 3 where key is your set's name.
Click to reveal answer
intermediate
What happens if you ask SRANDMEMBER for more elements than the set contains?
Redis returns as many unique elements as possible, up to the set size, without duplicates.
Click to reveal answer
intermediate
How can you get random elements with possible duplicates using SRANDMEMBER?
Use a negative count like SRANDMEMBER key -3. This allows duplicates in the result.
Click to reveal answer
beginner
Does SRANDMEMBER remove elements from the set?
No, it only returns random elements without removing them from the set.
Click to reveal answer
What does SRANDMEMBER myset return if myset contains {"apple", "banana", "cherry"}?
ARemoves and returns one element
BAll elements in random order
COne random element from the set
DReturns the size of the set
✗ Incorrect
SRANDMEMBER returns one random element without removing it.
How do you get 2 random elements from a Redis set named fruits?
ASRANDMEMBER fruits 2
BSRANDMEMBER fruits -2
CSPOP fruits 2
DSMEMBERS fruits 2
✗ Incorrect
Use SRANDMEMBER fruits 2 to get 2 unique random elements without removal.
What does SRANDMEMBER key -3 do?
AReturns the first 3 elements
BReturns 3 unique random elements
CRemoves 3 elements randomly
DReturns 3 random elements with possible duplicates
✗ Incorrect
Negative count allows duplicates in the returned random elements.
If a set has 2 elements and you request 5 unique elements with SRANDMEMBER, what happens?
AReturns 2 unique elements
BReturns 5 elements with duplicates
CReturns an error
DReturns empty
✗ Incorrect
Redis returns as many unique elements as possible, up to the set size.
Does SRANDMEMBER change the set contents?
AYes, it removes returned elements
BNo, it only reads random elements
CYes, it adds new elements
DYes, it sorts the set
✗ Incorrect
SRANDMEMBER does not remove or modify the set.
Explain how to use SRANDMEMBER to get random elements from a Redis set and how the count parameter affects the output.
Think about how you pick random cards from a deck without removing them.
You got /5 concepts.
Describe the difference between SRANDMEMBER and SPOP in Redis.
Compare looking at a card vs taking it out of the deck.
You got /5 concepts.
Practice
(1/5)
1. What does the Redis command SRANDMEMBER do when used on a set?
easy
A. Returns one or more random elements from the set without removing them
B. Removes and returns one or more random elements from the set
C. Returns all elements in the set in random order
D. Counts the number of elements in the set
Solution
Step 1: Understand SRANDMEMBER command purpose
The command is designed to pick random elements from a Redis set without deleting them.
Step 2: Compare with other commands
Unlike SPOP, which removes elements, SRANDMEMBER only returns random elements but keeps the set intact.
Final Answer:
Returns one or more random elements from the set without removing them -> Option A
Quick Check:
SRANDMEMBER = random pick without removal [OK]
Hint: SRANDMEMBER picks random elements but does not remove them [OK]
Common Mistakes:
Confusing SRANDMEMBER with SPOP which removes elements
Thinking it returns all elements randomly
Assuming it counts elements
2. Which of the following is the correct syntax to get 3 random elements from a Redis set named myset using SRANDMEMBER?
easy
A. SRANDMEMBER myset -3
B. SRANDMEMBER 3 myset
C. SRANDMEMBER myset COUNT 3
D. SRANDMEMBER myset 3
Solution
Step 1: Recall SRANDMEMBER syntax
The correct syntax is SRANDMEMBER key [count], where count is optional and comes after the key.
Step 2: Identify correct option
SRANDMEMBER myset 3 matches this syntax: SRANDMEMBER myset 3 to get 3 random elements.
Final Answer:
SRANDMEMBER myset 3 -> Option D
Quick Check:
SRANDMEMBER key count = correct syntax [OK]
Hint: Count parameter comes after the key in SRANDMEMBER [OK]
Common Mistakes:
Placing count before the key
Using COUNT keyword which is invalid
Using negative count without understanding effect
3. Given a Redis set colors containing {"red", "green", "blue"}, what could be the result of SRANDMEMBER colors 2?
medium
A. ["red", "green"]
B. ["red", "red"]
C. ["yellow", "blue"]
D. ["green"]
Solution
Step 1: Understand SRANDMEMBER with positive count
When count is positive, SRANDMEMBER returns that many unique random elements from the set.
Step 2: Analyze possible outputs
Since the set has "red", "green", "blue", the result must be two unique elements from these. ["red", "green"] shows two valid unique elements. ["red", "red"] repeats "red" which is not possible with positive count. ["yellow", "blue"] includes "yellow" which is not in the set. ["green"] returns only one element, but count requested is 2.
Final Answer:
["red", "green"] -> Option A
Quick Check:
Positive count = unique random elements [OK]
Hint: Positive count returns unique elements, no repeats [OK]
Common Mistakes:
Expecting repeated elements with positive count
Including elements not in the set
Returning fewer elements than requested
4. You run SRANDMEMBER myset -2 on a set with elements {"a", "b", "c"}, but get an error. What is the likely cause?
medium
A. Negative count is not allowed in SRANDMEMBER
B. The set myset does not exist
C. The Redis version does not support negative count
D. Negative count returns repeated elements, no error
Solution
Step 1: Understand negative count behavior
Negative count in SRANDMEMBER returns elements allowing repeats (sampling with replacement), but this feature requires Redis 3.2 or newer.
Step 2: Identify cause of error
If an error occurs, it is likely because the Redis version is older and does not support negative count.
Final Answer:
The Redis version does not support negative count -> Option C
5. You want to randomly select 5 elements from a Redis set players which contains only 3 unique names, allowing repeats. Which command correctly achieves this?
hard
A. SRANDMEMBER players 5
B. SRANDMEMBER players -5
C. SPOP players 5
D. SRANDMEMBER players COUNT 5
Solution
Step 1: Understand requirement for repeats
To get 5 elements allowing repeats from a set of 3, we need sampling with replacement.
Step 2: Identify correct SRANDMEMBER usage
Using a negative count like -5 allows repeated elements in the result.
Step 3: Eliminate other options
SRANDMEMBER players 5 returns only unique elements, so max 3. SPOP players 5 removes elements, not desired. SRANDMEMBER players COUNT 5 uses invalid syntax.
Final Answer:
SRANDMEMBER players -5 -> Option B
Quick Check:
Negative count = repeated random elements [OK]
Hint: Use negative count for repeats in SRANDMEMBER [OK]
Common Mistakes:
Using positive count expecting repeats
Using SPOP which removes elements
Using invalid syntax with COUNT keyword
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.