×i½uÔÚÑо¿¢ºi°uµÄ¹¦ÄÜ£¬ÓÚÊÇдÁ˸oºi°uµÄÉu³ÉËa¨¡£
*ºi°uÉu³ÉËa¨µÄÐeÇo
*Ô¤ÏÈÉu³ÉËuÓеĺi°u»¹ÊÇÒ»¸oÇeÇoËae»uÉu³ÉÒ»¸oºi°u
¼oµ¥À´Ëµ£¬¾ÍÊÇ°ÑÒ»¸o´oÕuÊýmÖ½a£¨Ö±½ÓÒÔ¡°ÖΪµ¥Î»£¬Èç1Ôª¼´100£(C)Ö½a³Én¸oСÕuÊýµÄ¹ý³Ì£¬Ð¡ÕuÊýµÄ¶Î§ÊÇ[min, max]¡£
×i¼oµ¥µÄ˼£¬Ïȱ£µ×£¬Ã¿¸oСºi°u±£Ö¤ÓÐmin£¬È»ºoÿ¸oÇeÇo¶¼Ëae»uÉu³ÉÒ»¸o0µ½(max-min)¶Î§µÄÕuÊý£¬ÔÙ¼ÓÉÏmin¾ÍÊǺi°uµÄÇ®Êý¡£
Õa¸oËa¨ËaÈ»¼oµ¥£¬µ«ÊÇÓÐÒ»¸o±×¶Ë£º×iºoÉu³ÉµÄºi°u¿ÉÄܶ¼ÊÇminÇ®ÊýµÄ¡£Ò²¾ÍÊÇ˵¿ÉÄÜ×iºoµÄºi°u¶¼ÊÇ0.01ÔªµÄ¡£
ÁiÒ»ÖÖ*½Ê½ÊÇÔ¤ÏÈÉu³ÉËuÓкi°u£¬ÕaÑu¾Í±È½ÏÈÝÒ׿ØÖÆÁË¡£ÎÒÑ¡ÔñµÄÊÇÔ¤ÏÈÉu³ÉËuÓеĺi°u¡£
*ÀiÏeµÄºi°uÉu³ÉËa¨
*ÀiÏeµÄºi°uÉu³É½a¹uÊÇƽ¾uÖµ¸½½uµÄºi°u±È½Ï¶a£¬´oºi°uºÍСºi°uµÄÊýÁ¿±È½ÏÉÙ¡£
¿ÉÒÔÏeÏñÏ£¬Éu³Éºi°uµÄÊýÁ¿µÄÖ²¼ÓеaÏñÕý̬*Ö²¼¡£
ÄÇôÈçºÎʵÏÖÕaÖÖƽ¾uÏ߸½½uÖµ±È½Ï¶aµÄÒªÇoÄØ£¿
¾ÍÊÇÒªÕÒµ½Ò»ÖÖËa¨£¬¿ÉÒÔÌa¸ßƽ¾uÖµ¸½½uµÄ¸ÅÂÊ¡£ÄÇôÀuÓÃÒ»ÖÖ¡±ÅoÕÍ¡°ÔÙ¡±ÊÕËo¡°µÄ½Ê½À´´iµ½ÕaÖÖЧ¹u¡£
ÏÈƽ½£¬ÔÙÉu³Éƽ½¶Î§ÄÚµÄËae»uÊý£¬ÔÙ¿ª½£¬ÄÇô¸ÅÂʾͲ»ÔÙÊÇƽ¾uµÄÁË¡£
¾ßÌaËa*¨£º£¨ÉeÖõÄ×ÜÇ®Êý£¬×ÜÈËÊý£¬×i´oÖµ£¬×iСֵҪºÏÀi£(C)
Php´uÂe
/**
* ÇoÒ»¸oÊýµÄƽ*½
* @param $n
*/
function sqr($n){
return $n*$n;
}
/**
* Éu²uminºÍmaxÖ®¼aµÄËae»uÊý£¬µ«ÊǸÅÂʲ»ÊÇƽ¾uµÄ£¬´Óminµ½max*½Ïo¸ÅÂÊÖð½¥¼Ó´o¡£
* ÏÈƽ*½£¬È»ºo²uÉuÒ»¸oƽ*½Öµ*¶Î§ÄÚµÄËae»uÊý£¬ÔÙ¿ª*½£¬ÕaÑu¾Í²uÉuÁËÒ»ÖÖ¡°ÅoÕÍ¡±ÔÙ¡°ÊÕËo¡±µÄЧ¹u¡£
*/
function xRandom($bonus_min,$bonus_max){
$sqr = intval(sqr($bonus_max-$bonus_min));
$rand_num = rand(0, ($sqr-1));
return intval(sqrt($rand_num));
}
/**
*
* @param $bonus_total ºi°u×ܶi
* @param $bonus_count ºi°u¸oÊý
* @param $bonus_max ÿ¸oСºi°uµÄ×i´o¶i
* @param $bonus_min ÿ¸oСºi°uµÄ×iС¶i
* @return ´ae*ÅÉu³ÉµÄÿ¸oСºi°uµÄÖµµÄһάÊý×e
*/
function getBonus($bonus_total, $bonus_count, $bonus_max, $bonus_min) {
$result = array();
$average = $bonus_total / $bonus_count;
$a = $average - $bonus_min;
$b = $bonus_max - $bonus_min;
//
//ÕaÑuµÄËae»uÊýµÄ¸ÅÂÊʵ¼Ê¸Ä±aÁË£¬²uÉu´oÊýµÄ¿ÉÄÜÐÔÒª±È²uÉuСÊýµÄ¸ÅÂÊҪС¡£
//ÕaÑu¾ÍʵÏÖÁË´o²¿*Öºi°uµÄÖµÔÚƽ¾uÊý¸½½u¡£´oºi°uºÍСºi°u±È½ÏÉÙ¡£
$range1 = sqr($average - $bonus_min);
$range2 = sqr($bonus_max - $average);
for ($i = 0; $i < $bonus_count; $i++) {
//ÒoΪСºi°uµÄÊýÁ¿Í¨³£ÊÇÒª±È´oºi°uµÄÊýÁ¿Òª¶aµÄ£¬ÒoΪÕaÀiµÄ¸ÅÂÊÒªµ÷»»¹ýÀ´¡£
//µ±Ëae»uÊý>ƽ¾uÖµ£¬Ôo²uÉuСºi°u
//µ±Ëae»uÊý<ƽ¾uÖµ£¬Ôo²uÉu´oºi°u
if (rand($bonus_min, $bonus_max) > $average) {
// ÔÚƽ¾uÏßÉϼoÇ®
$temp = $bonus_min + xRandom($bonus_min, $average);
$result[$i] = $temp;
$bonus_total -= $temp;
} else {
// ÔÚƽ¾uÏßÉϼÓÇ®
$temp = $bonus_max - xRandom($average, $bonus_max);
$result[$i] = $temp;
$bonus_total -= $temp;
}
}
// Èç¹u»¹ÓÐÓaÇ®£¬Ôo³¢ÊÔ¼Óµ½Ð¡ºi°uÀi£¬Èç¹u¼Ó²»½øÈ¥£¬Ôo³¢ÊÔÏÂÒ»¸o¡£
while ($bonus_total > 0) {
for ($i = 0; $i < $bonus_count; $i++) {
if ($bonus_total > 0 && $result[$i] < $bonus_max) {
$result[$i]++;
$bonus_total--;
}
}
}
// Èç¹uÇ®ÊǸºÊýÁË£¬»¹µÃ´ÓÒÑÉu³ÉµÄСºi°uÖгeÈ¡»ØÀ´
while ($bonus_total < 0) {
for ($i = 0; $i < $bonus_count; $i++) {
if ($bonus_total < 0 && $result[$i] > $bonus_min) {
$result[$i]--;
$bonus_total++;
}
}
}
return $result;
}
$bonus_total = 200;
$bonus_count = 100;
$bonus_max = 10;//´ËËa*¨ÒªÇoÉeÖõÄ×i´oÖµÒª´oÓÚƽ¾uÖµ
$bonus_min = 1;
$result_bonus = getBonus($bonus_total, $bonus_count, $bonus_max, $bonus_min);
$total_money = 0;
$arr = array();
foreach ($result_bonus as $key => $value) {
$total_money += $value;
if(isset($arr[$value])){
$arr[$value] += 1;
}else{
$arr[$value] = 1;
}
}
//Êa³o×ÜÇ®Êý£¬²e¿´ÊÇ*ñÓeÉeÖõÄ×ÜÊýÏaͬ
echo $total_money;
//Êa³oËuÓÐËae»uºi°uÖµ
var_dump($result_bonus);
//ͳ¼Æÿ¸oÇ®ÊýµÄºi°uÊýÁ¿£¬¼i²eÊÇ*ñ½Ó½uÕý̬*Ö²¼
ksort($arr);
var_dump($arr);
ÒÔÉϾÍÊDZ¾ÎĵÄÈ«²¿ÄÚÈÝ£¬Ï£Íu¶Ô´o¼ÒµÄѧϰÓÐËu°iÖu£¬Ò²Ï£Íu´o¼Ò¶a¶aÖ§³Ö½Å±¾Ö®¼Ò¡£