Home  >  Article  >  Database  >  Use Redis to complete the lottery function

Use Redis to complete the lottery function

齐天大圣
齐天大圣Original
2020-05-13 16:21:163937browse

Introduction to Redis collection

Redis’ collection data type is very powerful. Speaking of sets, you may think of sets in high school mathematics. In fact, they have the same meaning. Redis collections can store many strings (elements). Redis supports up to 2 to the 32nd power minus 1 element, but the elements in the collection are unique and there will be no duplication. Like sets in mathematics, Redis also supports intersection, union and difference.

You can use it to accomplish many interesting functions. The most common one is the tag function. Maybe user A’s tags include “animation”, “sports”, and “two-dimensional”, and user B’s tags include “sports”, “travel”, and “basketball”. Then, using the union of sets, you can know what their common label is. In addition, when the system knows the user's tags, it can recommend relevant advertisements or products to them. In addition, it can also implement many interesting functions. Today, let’s take a look at how to use Reids to implement the lottery function.

sRandMember and sPop

The functions of these two commands are very similar. They both return an element value from the collection. The difference is that sRandMember will not delete the returned elements from the collection, but sPop will. These two commands can implement different lottery algorithms respectively.

For example, there are 100 elements in the set, and the values ​​range from the number 1 to the number 100. We define that if the number 1 is drawn, it means winning.

If you use sRandMember, no matter how many times you have drawn it before, the probability of winning the next time is 1%. With sPop, the probability of winning is different every time. The first person's probability of winning is 1%. If the first person fails to win, the second person's probability of winning is 1/99, and so on.

Lottery function implementation

There are actually only two steps to implement the lottery function. First, set the lottery probability, that is, add elements to the set, and then The lottery has begun.

Set the lottery probability, the pseudo code is as follows:

/**
 * $key 集合键名
 * $cnt 集合元素数量
 */
function setProb($key, $cnt)
{
    for ($i = 1; $i <= $cnt; $i ++) {
        $redis->sAdd($key, $i);
    }
}

Lottery, the pseudo code is as follows:

/**
 * string $key 集合键名
 * int $stand 小于等于该数即表示抽中
 * int $type 抽奖算法,1表示使用sRandMember,2的话
 *           使用sPop
 */
function draw ($key, $stand, $type = 1)
{
    if ($type == 1) {
        $number = $redis->sRandMember($key);
    } else {
        $number = $redis->sPop($key);
    }
    
    return $number < $stand;
}

Note that stand is used to set the probability. For example, if there are 10,000 elements in the collection, and stand is set to 10, then the probability is 10/10,000. When the element value returned by the redis collection is less than or equal to this value, it means that it is selected.

Redis collections can also perform other interesting functions, such as counting the access IPs of the day, counting active users, etc. Everyone can use their imagination to complete more interesting functions.

The above is the detailed content of Use Redis to complete the lottery function. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn