当前位置:网站首页>Thinkphp+redis realizes simple lottery
Thinkphp+redis realizes simple lottery
2022-07-03 01:19:00 【Life goes on and battles go on】
Redis Collection Introduction
Redis Set data type of , Very powerful . Speaking of assembly , You may think of the set in high school mathematics . In fact, they meant the same thing .Redis Many strings can be stored in the set ( Elements ),Redis Most support 2 Of 32 The power minus 1 Elements , But the elements in the set are unique , There will be no repetition . Just like set in Mathematics ,Redis Also supports intersection , Union and subtraction .
It can complete many interesting functions . The most common is the tag function , Probably A The user's tag has “ Comic ”、” motion “、” Secondary yuan “,B The user's tag has ” motion “、” tourism “、” Basketball “. that , Use the union of sets , We can know what their common label is . in addition , When the system knows the user's label , You can recommend relevant advertisements or products to them . besides , It can also realize many interesting functions . today , Let's see how to use it Reids Realize the lottery function .
sRandMember、sPop
The functions of these two commands are very similar , All return an element value from the collection . The difference is ,sRandMember The returned elements will not be deleted from the collection , however sPop Will delete . These two commands can implement different lottery algorithms .
such as , There are 100 Elements , Value from number 1 To digital 100. We define what we draw as numbers 1 Words , It means winning the prize .
Use sRandMember Words , No matter how many times I smoked before , The probability of winning the next time is 1%. While using sPop Words , Then the probability of winning each time is different . The probability of the first person winning is 1%, When the first person doesn't win , The second person's probability of winning is 1/99, And so on .
The lottery function is realized
There are only two steps to realize the lottery function , First, set the lottery probability , That is, add elements to the set , Then the lottery begins .
Set the lottery probability , The pseudocode is as follows :
// sweepstakes , Get an exchange code Definition 1-10 Win the prize
public function create($key = 'draw', $type = 'nil')
{
//tp6 load redis
$redis = Cache::store('redis')->handler();
// Winning logic
if ($type == 'nil') {
// The probability of ordinary users is small Algorithm No duplication To generate exchange code
$num = mt_rand(7, 9999);
} else if ($type == 'VIP') {
$num = mt_rand(3, 1000);
} else if ($type == 'VIP8') {
$num = mt_rand(1, 2);
}
$redis->sAdd($key, $num);
return $num;
} // A prize Lottery only Don't write logic
public function read($key, $stand = 10)
{
$bool = $this->draw($key, $stand = 10);
// Lottery results for users
return $bool ? ' Congratulations on winning the prize , The son of the chosen ' : ' unfortunately , Nearly 100 million points won the prize ';
}
// Write the lottery logic
public function draw($key, $stand)
{
$redis = Cache::store('redis')->handler();
// Judge whether there is any lottery content in the collection
if ($redis->scard($key) > 0) {
// Real random lottery
$number = $redis->spop($key);
} else {
echo " The draw is over ";
exit();
}
echo $number . "<hr/>";
return $number < $stand;
}Be careful : Route parameters
边栏推荐
- Basic remote connection tool xshell
- 软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
- JDBC courses
- [shutter] image component (configure local GIF image resources | load placeholder with local resources)
- 按键精灵打怪学习-自动回城路线的判断
- excel表格计算时间日期的差值,并转化为分钟数
- 寻找标杆战友 | 百万级实时数据平台,终身免费使用
- [self management] time, energy and habit management
- 如今少年已归来,人间烟火气最抚凡人心 复工了~
- ThinkPHP+Redis实现简单抽奖
猜你喜欢

Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
![[shutter] image component (cached_network_image network image caching plug-in)](/img/cc/967ff62c7f82e1c6613b3d0f26bb3e.gif)
[shutter] image component (cached_network_image network image caching plug-in)

MySQL --- 数据库查询 - 条件查询

Matlab Doppler effect produces vibration signal and processing

Database SQL language 01 where condition

matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决

(C语言)数据的存储
![[C language] branch and loop statements (Part 1)](/img/47/6efcc59bd26e26f66c698635c26c8b.png)
[C language] branch and loop statements (Part 1)

电话网络问题

leetcode:701. 二叉搜索树中的插入操作【bst的插入】
随机推荐
[自我管理]时间、精力与习惯管理
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]
[untitled]
[flutter] icons component (load the built-in icon of flutter | display the material design icon completely)
拥抱平台化交付的安全理念
异步、邮件、定时三大任务
Explain the basic concepts and five attributes of RDD in detail
每日一题之干草堆的移动
【系统分析师之路】第五章 复盘软件工程(开发模型开发方法)
Create your first Kivy program Hello word (tutorial includes source code)
Makefile中wildcard、patsubst、notdir的含义
MySQL foundation 06 DDL
电话网络问题
对非ts/js文件模块进行类型扩充
[FPGA tutorial case 5] ROM design and Implementation Based on vivado core
What is needed to develop a domestic arm intelligent edge computing gateway
Embrace the safety concept of platform delivery
R language ggplot2 visual faceting, visual facet_wrap bar plot, using strip Text function customize the size of the strip of each facet title in the facet graph (cutimi
Strongly connected components of digraph
465. 最优账单平衡 DFS 回溯