Sunday, 12 April 2015

SRANDMEMBER key count : Get random element from set

Syntax
SRANDMEMBER key count
   Returns the random element from the set, when called with only key argument.

If count > 0 and count < NumberOfElements in the set
    Returns array of count distinct elements.

If count > 0 and count > NumberOfElements in the set
    Returns all the elements in the set.

If count< 0
    Returns array of count elements, elements can be repeated.

127.0.0.1:6379> sadd set1 1 3 5 7 2 4 6 8 
(integer) 8 
127.0.0.1:6379> srandmember set1 
"5" 
127.0.0.1:6379> srandmember set1 4 
1) "7" 
2) "1" 
3) "4" 
4) "5" 
127.0.0.1:6379> srandmember set1 9 
1) "1" 
2) "2" 
3) "3" 
4) "4" 
5) "5" 
6) "6" 
7) "7" 
8) "8" 
127.0.0.1:6379> srandmember set1 -9 
1) "1" 
2) "4" 
3) "3" 
4) "5" 
5) "2" 
6) "2" 
7) "7" 
8) "1" 
9) "8" 
127.0.0.1:6379> 

 
Prevoius                                                 Next                                                 Home

No comments:

Post a Comment