当前位置:网站首页>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
边栏推荐
- leetcode:701. 二叉搜索树中的插入操作【bst的插入】
- 【C语言】指针与数组笔试题详解
- 1038 Recover the Smallest Number
- 465. 最优账单平衡 DFS 回溯
- 删除有序链表中重复的元素-II
- Appuyez sur l'apprentissage de l'esprit de frappe - reconnaissance des coordonnées de fond multithreadées
- [C language] branch and loop statements (Part 1)
- Data analysis, thinking, law breaking and professional knowledge -- analysis method (I)
- Specified interval inversion in the linked list
- matlab查找某一行或者某一列在矩阵中的位置
猜你喜欢

Embrace the safety concept of platform delivery

电话网络问题

无向图的割点

leetcode 6103 — 从树中删除边的最小分数

Strongly connected components of digraph

Infrared thermography temperature detection system based on arm rk3568
![[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)

JS inheritance and prototype chain

MySQL basics 03 introduction to MySQL types

基本远程连接工具Xshell
随机推荐
Trois tâches principales: asynchrone, courrier et timing
Specified interval inversion in the linked list
Leetcode 6103 - minimum fraction to delete an edge from the tree
Draw love with go+ to express love to her beloved
【无标题】
Assets, vulnerabilities, threats and events of the four elements of safe operation
寻找标杆战友 | 百万级实时数据平台,终身免费使用
异步、邮件、定时三大任务
[shutter] image component (configure local GIF image resources | load placeholder with local resources)
Androd Gradle 对其使用模块依赖的替换
leetcode:871. 最低加油次数【以前pat做过 + 最大堆 +贪心】
异步、郵件、定時三大任務
First hand evaluation of Reza electronics rz/g2l development board
leetcode:701. 二叉搜索树中的插入操作【bst的插入】
每日一题之干草堆的移动
Kivy tutorial how to create drop-down lists in Kivy
Find a benchmark comrade in arms | a million level real-time data platform, which can be used for free for life
Compare version number
按键精灵打怪学习-多线程后台坐标识别
FPGA - 7 Series FPGA internal structure clocking -04- multi area clock