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!

Redis是现在最热门的key-value数据库,Redis的最大特点是key-value存储所带来的简单和高性能;相较于MongoDB和Redis,晚一年发布的ES可能知名度要低一些,ES的特点是搜索,ES是围绕搜索设计的。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于redis的一些优势和特点,Redis 是一个开源的使用ANSI C语言编写、遵守 BSD 协议、支持网络、可基于内存、分布式存储数据库,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis Cluster集群收缩主从节点的相关问题,包括了Cluster集群收缩概念、将6390主节点从集群中收缩、验证数据迁移过程是否导致数据异常等,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于原子操作中命令原子性的相关问题,包括了处理并发的方案、编程模型、多IO线程以及单命令的相关内容,下面一起看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了Redis实现排行榜及相同积分按时间排序,本文通过实例代码给大家介绍的非常详细,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了bitmap问题,Redis 为我们提供了位图这一数据结构,位图数据结构其实并不是一个全新的玩意,我们可以简单的认为就是个数组,只是里面的内容只能为0或1而已,希望对大家有帮助。

本篇文章给大家带来了关于redis的相关知识,其中主要介绍了关于实现秒杀的相关内容,包括了秒杀逻辑、存在的链接超时、超卖和库存遗留的问题,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software
