当前位置:网站首页>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
边栏推荐
- 按键精灵打怪学习-回城买药加血
- The R language uses the ctree function in the party package to build conditional inference decision trees, uses the plot function to visualize the trained conditional inference decision tree, and the
- Kivy教程大全之如何在 Kivy 中创建下拉列表
- Key wizard play strange learning - front desk and Intranet send background verification code
- matlab将数字矩阵保存为地理空间数据出错,显示下标索引必须为正整数类型或逻辑类型,解决
- 异步、郵件、定時三大任務
- Strongly connected components of digraph
- Arduino DY-SV17F自动语音播报
- How to convert Quanzhi a40i/t3 to can through SPI
- d,ldc构建共享库
猜你喜欢
Explain the basic concepts and five attributes of RDD in detail
【C语言】指针与数组笔试题详解
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
1696C. Fishingprince Plays With Array【思维题 + 中间状态 + 优化存储】
安全运营四要素之资产、脆弱性、威胁和事件
Asynchronous, email and scheduled tasks
Infrared thermography temperature detection system based on arm rk3568
[Androd] Gradle 使用技巧之模块依赖替换
寻找标杆战友 | 百万级实时数据平台,终身免费使用
leetcode 6103 — 从树中删除边的最小分数
随机推荐
MySQL
2022 Jiangxi Provincial Safety Officer B certificate reexamination examination and Jiangxi Provincial Safety Officer B certificate simulation examination question bank
MySQL foundation 06 DDL
[system analyst's road] Chapter V double disk software engineering (development model development method)
按键精灵打怪学习-多线程后台坐标识别
ThinkPHP+Redis实现简单抽奖
d,ldc构建共享库
Infrared thermography temperature detection system based on arm rk3568
Leetcode 6103 - minimum fraction to delete an edge from the tree
基本远程连接工具Xshell
excel去除小数点后面的数据,将数字取整
每日一题之干草堆的移动
Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
MySQL basic usage 02
Niu Ke swipes questions and clocks in
465. 最优账单平衡 DFS 回溯
1696C. Fishingprince plays with array [thinking questions + intermediate state + optimized storage]
软考信息系统项目管理师_历年真题_2019下半年错题集_上午综合知识题---软考高级之信息系统项目管理师053
Excel removes the data after the decimal point and rounds the number
Foundations of data science is free to download