zoukankan      html  css  js  c++  java
  • Ts/Js 实现抽奖功能--根据物品权重计算

      /**
         * 
         * @param vWeight 权重的数组:例如【1,1,1,1,1】
         * @returns //注意:返回的下标加1(占第几个)
         */
      public static GetPrize(vWeight: number[]): number {
            //计算权重之和  prev 是前一次累加后的数值,currVal 是本次待加的数值
            let weightSum = vWeight.reduce((prev, currVal) => {
                return prev + currVal;
            }, 0);
            let random = Math.ceil(Math.random() * weightSum);
            console.log("random ---->" + random);
            let count = 0;
            for (let i = 0; i < vWeight.length; i++) {
                count += vWeight[i];
                if (random <= count) {
                    return i + 1;
                }
            }
        }
    山重水复疑无路,柳暗花明又一村! 专注填坑,少走弯路!
  • 相关阅读:
    hdu 3496
    poj 2374
    zoj 3399
    poj 1321
    sgu 365
    hdu 3555
    poj 3345
    poj 2355
    Android重命名文件
    在workflow中传值的sample
  • 原文地址:https://www.cnblogs.com/mqflive81/p/15061115.html
Copyright © 2011-2022 走看看