This article describes the implementation method of php imitation WeChat red envelope distribution algorithm. Share it with everyone for your reference, the details are as follows:
/** * 红包分配:把一定金额随机分配给指定人数 * * @param int $money 用于分配的金额 * @param int $num 分配人数 */ function RandomMoney($money, $num) { echo "$money元随机分成$num份分别是:<br/>"; $remain=$money; $use=0; for ($i=1; $i<$num; $i++) { $temp=rand(0,$remain*100)/100; echo "余额:".$remain.",已使用:".$use.",分配:".$temp."<br/>"; $use+=$temp; $remain-=$temp; } echo "余额:".$remain.",已使用:".$use.",分配:".$remain; } echo RandomMoney(10,5);
PS:The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:
PHP code online formatting and beautification tool: http://tools.jb51.net/code/phpformat
Readers who are interested in more PHP-related content can check out the special topics on this site: "Complete PHP Array Operation Skills", "Summary of PHP Sorting Algorithms", "Summary of PHP Common Traversal Algorithms and Techniques", "PHP Data Structure and Algorithm Tutorial", "php Programming Algorithm Summary", "PHP Mathematical Operation Skills Summary", "php Regular Expression Usage Summary", "PHP Operations and Operator Usage Summary", "php String Usage Summary" 》and《Summary of common database operation skills in php》
I hope this article will be helpful to everyone in PHP programming.