需求是这样的,根据可变参数 $people 来给每个人随机分配百分比,条件是人数一定会控制在 3
写了个简单的分配,但会出现最后一个人会得到很多的情况,并且由于分配区间实际上是 1 - 平均数 之间的百分比,不是严格意义上的 “随机平均分配”,求各位大大给个思路,抛砖引玉,不甚感谢。
$people = 3; //人数 $percent = 100; //百分比 $average = floor($percent/$people); $rand_array = array(); $count = 0; for ($i=0; $i < $n; $i++) { if($i == ($n - 1)){ $rand_array[$i] = 100 - $count; }else{ $rand_array[$i] = rand(1,$average); $count = $count + $rand_array[$i]; } } return $rand_array;
回复讨论(解决方案)
这样可能好点
$people = 3; //人数$percent = 100; //百分比$res = array_fill(0, 3, floor($percent/$people)); //平均分配$d = 5; //容差foreach($res as &$v) $v += rand(-$d, $d);$res[rand(0, $people-1)] += $percent - array_sum($res); //随机将残差补入print_r($res);
忘记上来结贴了,感谢版主大大的回复,问题已经解决了,贴出来与大家分享:
public static function rand_bouns($person){ //百分比 $percent = 100; $now_person = $person; $bouns = array(); for($i=0;$i<=$person-1;$i++){ $bouns[$i] = self::get_bouns($now_person,$percent); $percent = $percent - $bouns[$i]; $now_person = $now_person - 1; $now_bouns += $bouns[$i]; } return $bouns; }public static function get_bouns($person,$percent){ if($person==1) return $percent; $max = 30; if($percent < $max) $max = $percent; $min = $percent-$max*($person-1) <= 0 ? 1 : $percent-$max*($person-1); $max = $max-($person) <= 0 ? 1 : $max-($person); return rand($min,$max); }

APHPDependencyInjectionContainerisatoolthatmanagesclassdependencies,enhancingcodemodularity,testability,andmaintainability.Itactsasacentralhubforcreatingandinjectingdependencies,thusreducingtightcouplingandeasingunittesting.

Select DependencyInjection (DI) for large applications, ServiceLocator is suitable for small projects or prototypes. 1) DI improves the testability and modularity of the code through constructor injection. 2) ServiceLocator obtains services through center registration, which is convenient but may lead to an increase in code coupling.

PHPapplicationscanbeoptimizedforspeedandefficiencyby:1)enablingopcacheinphp.ini,2)usingpreparedstatementswithPDOfordatabasequeries,3)replacingloopswitharray_filterandarray_mapfordataprocessing,4)configuringNginxasareverseproxy,5)implementingcachingwi

PHPemailvalidationinvolvesthreesteps:1)Formatvalidationusingregularexpressionstochecktheemailformat;2)DNSvalidationtoensurethedomainhasavalidMXrecord;3)SMTPvalidation,themostthoroughmethod,whichchecksifthemailboxexistsbyconnectingtotheSMTPserver.Impl

TomakePHPapplicationsfaster,followthesesteps:1)UseOpcodeCachinglikeOPcachetostoreprecompiledscriptbytecode.2)MinimizeDatabaseQueriesbyusingquerycachingandefficientindexing.3)LeveragePHP7 Featuresforbettercodeefficiency.4)ImplementCachingStrategiessuc

ToimprovePHPapplicationspeed,followthesesteps:1)EnableopcodecachingwithAPCutoreducescriptexecutiontime.2)ImplementdatabasequerycachingusingPDOtominimizedatabasehits.3)UseHTTP/2tomultiplexrequestsandreduceconnectionoverhead.4)Limitsessionusagebyclosin

Dependency injection (DI) significantly improves the testability of PHP code by explicitly transitive dependencies. 1) DI decoupling classes and specific implementations make testing and maintenance more flexible. 2) Among the three types, the constructor injects explicit expression dependencies to keep the state consistent. 3) Use DI containers to manage complex dependencies to improve code quality and development efficiency.

DatabasequeryoptimizationinPHPinvolvesseveralstrategiestoenhanceperformance.1)Selectonlynecessarycolumnstoreducedatatransfer.2)Useindexingtospeedupdataretrieval.3)Implementquerycachingtostoreresultsoffrequentqueries.4)Utilizepreparedstatementsforeffi


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 English version
Recommended: Win version, supports code prompts!

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor
