Bird
Raised Fist0
Redisquery~10 mins

SRANDMEMBER for random elements in Redis - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - SRANDMEMBER for random elements
Start with a Redis Set
↓
Call SRANDMEMBER command
↓
Check if count argument is given
↓
Return count random
↓
random elements from set
↓
Output the random element(s)
SRANDMEMBER picks one or more random elements from a Redis set without removing them.
Execution Sample
Redis
SADD fruits apple banana cherry date
SRANDMEMBER fruits 2
Add 4 fruits to a set, then get 2 random fruits from it.
Execution Table
StepCommandInputActionOutput
1SADDfruits apple banana cherry dateAdd 4 elements to set 'fruits'4 (number of elements added)
2SRANDMEMBERfruits 2Pick 2 random elements from 'fruits' set["banana", "date"] (example output)
3SRANDMEMBERfruitsPick 1 random element from 'fruits' set"apple" (example output)
💡 SRANDMEMBER returns random element(s) from the set without removing them.
Variable Tracker
VariableStartAfter SADDAfter SRANDMEMBER 2After SRANDMEMBER 1
fruits setempty[apple, banana, cherry, date][apple, banana, cherry, date][apple, banana, cherry, date]
SRANDMEMBER outputnonenone[banana, date]apple
Key Moments - 3 Insights
Does SRANDMEMBER remove elements from the set?
No, SRANDMEMBER only returns random elements but does not remove them, as shown in the variable_tracker where the 'fruits set' stays the same after SRANDMEMBER calls.
What happens if you call SRANDMEMBER without a count?
It returns a single random element as a string, not an array. This is shown in execution_table row 3 where only one element is returned.
Can SRANDMEMBER return duplicate elements when count is positive?
If count is positive and less than or equal to set size, elements are unique. If count is negative, duplicates can appear. This example uses positive count, so no duplicates.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what does SRANDMEMBER fruits 2 return at step 2?
AAll elements in the set
BOne random element from the set
CTwo random elements from the set
DAn error because count is too high
💡 Hint
Check the Output column in execution_table row 2.
According to variable_tracker, what happens to the 'fruits set' after SRANDMEMBER commands?
AIt loses elements
BIt stays the same
CIt doubles in size
DIt becomes empty
💡 Hint
Look at the 'fruits set' row in variable_tracker after SRANDMEMBER calls.
If SRANDMEMBER is called without count, what type of output is expected?
AA single string element
BAn array with one element
CAn empty array
DA number representing set size
💡 Hint
See execution_table row 3 Output column.
Concept Snapshot
SRANDMEMBER key [count]
- Returns random element(s) from a Redis set
- If count is positive, returns unique elements
- If count is negative, elements may repeat
- Does NOT remove elements from the set
- Without count, returns one element as string
Full Transcript
SRANDMEMBER is a Redis command to get random elements from a set. You start with a set of items. When you call SRANDMEMBER with a count, it returns that many random elements without removing them. If you call it without count, it returns one random element as a string. The set remains unchanged after calling SRANDMEMBER. This is useful when you want random samples but want to keep the original data intact.

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

  1. Step 1: Understand SRANDMEMBER command purpose

    The command is designed to pick random elements from a Redis set without deleting them.
  2. Step 2: Compare with other commands

    Unlike SPOP, which removes elements, SRANDMEMBER only returns random elements but keeps the set intact.
  3. Final Answer:

    Returns one or more random elements from the set without removing them -> Option A
  4. 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

  1. Step 1: Recall SRANDMEMBER syntax

    The correct syntax is SRANDMEMBER key [count], where count is optional and comes after the key.
  2. Step 2: Identify correct option

    SRANDMEMBER myset 3 matches this syntax: SRANDMEMBER myset 3 to get 3 random elements.
  3. Final Answer:

    SRANDMEMBER myset 3 -> Option D
  4. 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

  1. Step 1: Understand SRANDMEMBER with positive count

    When count is positive, SRANDMEMBER returns that many unique random elements from the set.
  2. 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.
  3. Final Answer:

    ["red", "green"] -> Option A
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    The Redis version does not support negative count -> Option C
  4. Quick Check:

    Negative count needs Redis 3.2+ [OK]
Hint: Negative count needs Redis 3.2+; older versions error [OK]
Common Mistakes:
  • Assuming negative count always errors
  • Ignoring Redis version compatibility
  • Thinking negative count is invalid syntax
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

  1. Step 1: Understand requirement for repeats

    To get 5 elements allowing repeats from a set of 3, we need sampling with replacement.
  2. Step 2: Identify correct SRANDMEMBER usage

    Using a negative count like -5 allows repeated elements in the result.
  3. 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.
  4. Final Answer:

    SRANDMEMBER players -5 -> Option B
  5. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard