Use php to solve a small algorithm and find a train of thought.

divide 10000 yuan into five parts, and the value of the difference between each one is different, and the difference of $d should be greater than 100.

Php
Mar.14,2021

Thank you for the invitation!

  1. sorts the random five digits, with the smallest in the first and the largest in the last.
  2. the first four times are random values
  3. 5th is the total minus the first four random values
  4. what matters is the range of random values for the first four times, that is, the minimum and maximum values. At this time, if there are more minimum and maximum limits, it is easier to generate random values later, and make sure that the minimum value is greater than 100 of the previous random value. The maximum value is much simpler, as small as possible, but not less than the minimum value. And will not be larger than the maximum value ($rand_max) used in my example below. (as to why you can think about it)
  5. if the difference in the generated random value duplicates, then the random value is regenerated
    <?php
    
    $sum  = 10000;
    $diff = 100;
    $count = 5;
    
    $return = array();
    $min    = 0;
    $i         = 1; 
    while($i < $count) {
    
        // 
        if(isset($return[$i]) || isset($return[$i+1])) {
            unset($return[$i]);
            unset($return[$i+1]);
        }
    
        // 
        $remain_sum = $sum - array_sum($return);
        // 
        $remain_count = intval($count - count($return));
        // :S = n(n+1)(2n+4)/12 + $diff * ((n-1)(n-2)/2)
        $min_diff = $remain_count * ($remain_count - 1) * (2 * ($remain_count - 1) + 4) / 12 + ($remain_count * ($remain_count - 1) / 2) * $diff;
        // 
        $min = $i == 1 ? 1 : $return[$i-1] + $diff + 1;
        
        // 
        mt_srand((double)microtime() * 1000000);
        // 
        $rand_max   = intval(($remain_sum - $min_diff) / $remain_count);
        // 
        $min        = mt_rand($min, $rand_max);
        // 
        $return[$i] = $min;
        if($i == $count - 1) {
            // 
            $return[$i+1] = $remain_sum - $min;
        }
        
        if(check_diff($return) === false) {
            $i--;
            continue;
        }
    
        $iPP;
    }
    
    var_dump($return);die;
    
    // 
    function check_diff($arr) {
        if(empty($arr)) {
            return false;
        }
        
        $arr = array_map('intval', $arr);
        sort($arr);
        
        $count = count($arr);
        $diff_arr = array();
        for($i = $count - 1; $i >= 0; $i --) {
            for ($j = 0; $j < $count; $j PP) {
                if($arr[$i] <= $arr[$j]) {
                    continue 2;
                }
                $diff_arr[] = $arr[$i] - $arr[$j];
            }    
        }
        
        if(count($diff_arr) === count(array_unique($diff_arr))) {
            return true;
        }
        
        return false;
    }

function splitMoney($money,$num,$difference){
    $arr = array();

    $v = $money / $num;

    for($i = 0;$i < $num - 1; $i PP){
        $cur = mt_rand($difference , $difference + ($difference/2));
        if($arr[$i] == 0){
            $arr[$i] = $v - ($cur/2);
            $arr[$i + 1] = $v + ($cur/2);
        }else{
            $arr[$i + 1] = $v + $cur;
        }
    }
 

    return $arr;
}
The

pseudo code is as above, and you need to deal with the problem that the sum is not equal to the actual amount.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e9f433-1a3c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e9f433-1a3c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?