当前位置:网站首页>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
边栏推荐
- MySQL foundation 06 DDL
- 465. DFS backtracking of optimal bill balance
- 电话网络问题
- Makefile中wildcard、patsubst、notdir的含义
- Merge K sorted linked lists
- 机器学习术语
- Explain the basic concepts and five attributes of RDD in detail
- Understanding and distinguishing of some noun concepts in adjustment / filtering
- R language uses coin package to apply permutation tests to independence problems (permutation tests, whether response variables are independent of groups, are two numerical variables independent, and
- excel IF公式判断两列是否相同
猜你喜欢
![leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]](/img/2c/8ec3926243fac8db9ed45d8053f3af.png)
leetcode:871. Minimum refueling times [Pat has done before + maximum stacking + greed]

Foundations of data science is free to download

matlab 多普勒效应产生振动信号和处理

攻克哈希的基本概念与实现

拥抱平台化交付的安全理念

Embrace the safety concept of platform delivery

Leetcode 6103 - minimum fraction to delete an edge from the tree

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

Excel if formula determines whether the two columns are the same

Leetcode 2097 - Legal rearrangement of pairs
随机推荐
d,ldc构建共享库
Strongly connected components of digraph
拥抱平台化交付的安全理念
电话网络问题
matlab查找某一行或者某一列在矩阵中的位置
Matlab saves the digital matrix as geospatial data, and the display subscript index must be of positive integer type or logical type. Solve the problem
Assets, vulnerabilities, threats and events of the four elements of safe operation
攻克哈希的基本概念与实现
Leetcode 6103 - minimum fraction to delete an edge from the tree
[shutter] image component (cached_network_image network image caching plug-in)
Detailed explanation of Q-learning examples of reinforcement learning
[Arduino experiment 17 L298N motor drive module]
MySQL基础用法02
Cut point of undirected graph
基本远程连接工具Xshell
按键精灵打怪学习-多线程后台坐标识别
Asynchronous, email and scheduled tasks
Arduino DY-SV17F自动语音播报
R language ggplot2 visualization: use ggplot2 to display dataframe data that are all classified variables in the form of thermal diagram, and customize the legend color legend of factor
leetcode:701. 二叉搜索树中的插入操作【bst的插入】