从原始数组中,随机添加数组的值,使得随机抽取的值符合传入值

从原始数组中,随机添加数组的值,使得随机抽取的值符合传入值
1.随机抽取的概率跟数字大小有关,数组数字越小,抽取的概率越大
2.返回的平均值average接近传入的price_pro 值即可
示例 1:

输入: good_price_list=[1,2,3,4,5,6,7,8,9,10,4,5,6,7,8,10]  price_pro = 5
输出:{ 
		good_price_list: [ 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 10, 10, 10 ],
	  	average: 4.953
  	 }

代码

class test{
	 constructor(){
        this.PROJECT_NAME = '公共'
        this.PROJECT_EN = ''
    }
    //获取列表抽取平均数
    get_list_average(good_price_list){
        var good_price_all = 0;
        good_price_list.forEach(e=>{
            let a = 1/e;
            a>1?a=1:null;
            good_price_all+=a;
        })
        let get_list_average = good_price_list.length/good_price_all;
        return { get_list_average, good_price_all };
    }

    genMat({baseList,baseNum}){
        var d = Math.random();
        var all = 0;
        var goodPrice = 0;

        baseList.forEach( e => {

            // var c = (1/e)*baseNum;
            let a = 1/e;
            a>1?a=1:null;
            var c = a*baseNum;


            if(d >= all){
                goodPrice = e
            }
            all += c;
        })
        return goodPrice;
    }

    /**
     * 生成达到目标抽取价格的商品价格列表
     * @param {Array} good_price 商品价格数组
     * @param {Array} price_pro 需要达到的平均抽取价格
     * 
     */
    gen_good_price(good_price, price_pro){
        //冒泡排序(小=》大)
        good_price.sort((a,b)=>{return a-b});
        let { get_list_average, good_price_all } = this.get_list_average(good_price);

        let minPrice = good_price[0]
        let maxPrice = good_price[good_price.length-1];
        // price_pro = Math.ceil(price_pro)
        price_pro = Number(price_pro.toFixed(5))
        console.log(`${this.PROJECT_NAME}=》gen_good_price price_pro ${price_pro} maxPrice${maxPrice} price_pro  minPrice${minPrice} get_list_average ${get_list_average}`)
        if(maxPrice - price_pro <= 0 || minPrice - price_pro >= 0 ){
            return  { good_price_list:good_price, average: get_list_average, good_price_all };
        }

        //列表复制
        let good_price_list = [...good_price];
        //偏差值
        let offset = 0.005;

        let { gen_arr, direct, numIndex } = this.gen_extract_list_obj(good_price_list,price_pro,offset);

        //获取抽取的数字
        let num ;
        while(true){

            let checkListAverage = this.checkListAverage(good_price_list,price_pro,direct,offset);
            get_list_average = checkListAverage.get_list_average;
            good_price_all = checkListAverage.good_price_all;
            console.log(`${this.PROJECT_NAME}=》 循环插入新数字num ${num}  get_list_average ${get_list_average} price_pro${price_pro}`);

            if(checkListAverage.result){
                console.log(`退出 checkListAverage `,checkListAverage)
                break;  
            }
            //获取抽取的数字
            num = gen_arr[numIndex] ;
            if(gen_arr.length >1){
                numIndex++;
                numIndex = numIndex%(gen_arr.length-1);
            }

            //对比新数字插入后的影响
            let compareLastListAverage = this.compareLastListAverage([...good_price_list,num],price_pro,Math.abs(get_list_average - price_pro));
            if(compareLastListAverage){
                console.log(`退出 compareLastListAverage${compareLastListAverage}`)
                break;
            }

           good_price_list.push(num);

        }
        console.log(`good_price_list.length  ${good_price_list.length}`)
        good_price_list.sort((a,b)=>{return a-b});
  
        return { good_price_list, average: get_list_average, good_price_all }

    }

   //生成抽取的价格列表
    gen_extract_list_obj(good_price_list,price_pro,offset){
        let { get_list_average, good_price_all } = this.get_list_average(good_price_list);

        //方向 
        let direct = 0;
        //抽取的列表数组
        let gen_arr = [];
        //抽取的列表数组定位
        let numIndex = 0;
        //去重列表
        // let distinct_good_list = [...new Set(good_price_list)];
        good_price_list.forEach(e=>{
            if(get_list_average - price_pro < -offset){
                direct = 1 ;  //偏移方向,变大
                if(e > price_pro){
                    gen_arr.push(e);
                }
            }else if(get_list_average - price_pro > offset){
                direct = -1 ;  //偏移方向,变小
                if(e < price_pro){
                    gen_arr.push(e);
                }
            }
        })

        return { gen_arr, direct, numIndex }


    }
    //检测列表平均值是否达到目标
    checkListAverage(good_price_list,price_pro,direct,offset){

        let { get_list_average, good_price_all } = this.get_list_average(good_price_list);

       console.log(`get_list_average ${get_list_average} price_pro${price_pro}  direct ${direct}   ====  ${get_list_average - price_pro}`)
        if(
            ( (get_list_average - price_pro < -offset) && (direct == 1)  )  ||
            ( (get_list_average - price_pro >  offset) && (direct == -1)  )   
        ){
            return { get_list_average, good_price_all ,result: false};
        }else{
            return { get_list_average, good_price_all ,result: true};
        }
    }
    //对比新数字插入后的影响
    compareLastListAverage(good_price_list,price_pro,diff){
        let { get_list_average, good_price_all } = this.get_list_average(good_price_list);
        console.log(`diff ${diff}  Math.abs(get_list_average-price_pro) ${Math.abs(get_list_average-price_pro)}  get_list_average${get_list_average}`)
        if(diff < Math.abs(get_list_average-price_pro) ){
            return true;
        }else{
            return false;
        }
    }


}


var a = new test();
var good_price_list = [1,2,3,4,5,6,7,8,9,10,4,5,6,7,8,10];
let gen_pro = a.gen_good_price(good_price_list ,5)
console.log(gen_pro)  //返回符合条件的数组good_price_list 和抽取平均数average

// { 
//     good_price_list: [ 1, 2, 3, 4, 4, 5, 5, 6, 6, 6, 6, 6, 6, 6, 6, 7, 7, 7, 7, 7, 8, 8, 8, 8, 9, 10, 10, 10 ],
//       average: 4.953
//    }

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一杯码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值