源文地址:https://juejin.im/post/5ae413946fb9a07a9c03f7f7

<!DOCTYPE html>
<html lang="en"> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head> <body>
<script>
class RandomSplit {
constructor(num) {
// 实际总数
this.num = this.getNum(num);
// 放大倍数
try {
this.multiple = this.num.toString().split('.')[1].length;
} catch (e) {
this.multiple = 0;
}
// 用于整数运算的总数
this.calcNum = this.num * Math.pow(10, this.multiple);
}
// 判断是否为number(取用至“is-number”)
isNumber(num) {
let number = +num;
if ((number - number) !== 0) {
return false;
}
if (number === num) {
return true;
}
if (typeof num === 'string') {
if (number === 0 && num.trim() === '') {
return false;
}
return true;
}
return false;
}
// 获取数字
getNum(num, defaultNum = 0) {
return this.isNumber(num) ? (+num) : defaultNum;
}
//均分份数, 均分精度, 是否直接返回放大后的整数
average(n, precision, isInt) {
precision = Math.floor(this.getNum(precision, 0));
n = Math.floor(this.getNum(n));
let calcNum = this.calcNum * Math.pow(10, precision < 0 ? 0 : precision);
// 份数超过放大后的计算总数,即不够分的情况
if (n > calcNum) {
return [];
} else {
let index = 0;
// 平均数
let avg = Math.floor(calcNum / n);
// 剩余数
let rest = calcNum % n;
// 剩余数填充间隔
let gap = Math.round((n - rest) / rest) + 1;
// 原始平均数组
let result = Array(n).fill(avg);
//
while (rest > 0) {
index = (--rest) * gap;
result[index >= n ? (n - 1) : index]++;
}
// 返回放大后的结果数组
if (isInt) {
return result;
}
// 返回处理完符合精度要求的结果数组
return result.map((item) => {
return (item / Math.pow(10, this.multiple + precision));
});
}
}
// 随机划分的份数, 划分精度
split(n, precision) {
n = Math.floor(this.getNum(n));
precision = Math.floor(this.getNum(precision, 0));
// 均分
let arr = this.average(n, precision, true);
let arrResult = arr.concat();
for (let i = 0; i < arr.length; i++) {
//给出的总额
let num = Math.floor(Math.random() * arr[i]);
// 给左邻的数额
let numLeft = Math.floor(Math.random() * num);
// 给右邻的数额
let numRight = num - numLeft;
// 首尾index处理
let iLeft = i === 0 ? (arr.length - 1) : (i - 1);
let iRight = i === (arr.length - 1) ? 0 : (i + 1);
arrResult[i] -= num;
arrResult[iLeft] += numLeft;
arrResult[iRight] += numRight;
}
// 缩小至原尺度
return arrResult.map((item) => {
return (item / Math.pow(10, this.multiple + precision));
});
}
}
var r = new RandomSplit(250);
console.log(r.average(3))
console.log(r.split(3))
</script>
</body> </html>

  

js红包算法【转载】的更多相关文章

  1. PHP随机红包算法

    2017年1月14日 14:19:14 星期六 一, 整体设计 算法有很多种, 可以自行选择, 主要的"架构" 是这样的, 用redis decr()命令去限流, 用mysql去记 ...

  2. php 固定红包 + 随机红包算法

    <?php /** * 随机红包+固定红包算法[策略模式] * copyright (c) 2016 http://blog.csdn.net/CleverCode */ //配置传输数据DTO ...

  3. 微信红包算法TEST

    1.基本算法 设定总金额为10元,有N个人随机领取:N=1 则红包金额=X元: N=2 为保证第二个红包可以正常发出,第一个红包金额=0.01至9.99之间的某个随机数 第二个红包=10-第一个红包金 ...

  4. php红包算法函数[优化]

    php红包算法 <?php header("Content-Type: text/html;charset=utf-8");//输出不乱码,你懂的 $total=10000; ...

  5. php微信红包算法

    微信红包算法.php /**生成红包的函数*/ function getRandMoney($totalMoney, $totalPeople=2, $miniMoney=1){ $randRemai ...

  6. A* 寻路算法[转载]

    A* 寻路算法 转载地址:http://www.cppblog.com/christanxw/archive/2006/04/07/5126.html 原文地址: http://www.gamedev ...

  7. js的 算法 和 数据结构

    js的 算法 1.对一个对象数组按照对象某个属性进行排序  : https://www.cnblogs.com/webcabana/p/7460038.html 在做公交的项目中就碰到过这种算法问题, ...

  8. PHP实现简易微信红包算法

    <?php /** * PHP实现简易的微信红包算法 * @version v1.0 * @author quetiezheng */ function getMoney($total, $pe ...

  9. JAVA实现拼手气红包算法

    实现拼手气红包算法,有以下几个需要注意的地方: 抢红包的期望收益应与先后顺序无关 保证每个用户至少能抢到一个预设的最小金额,人民币红包设置的最小金额一般是0.01元,如果需要发其他货币类型的红包,比如 ...

随机推荐

  1. thinkphp使用with对关联数据进行预加载

    1.with('relation'),只预加载relation这个关联,如下面 public function relation() { return $this->hasOne(Relatio ...

  2. Oracle用imp导入dmp 提示遇到 ORACLE 错误 12560 TNS: 协议适配器错误 解决方法

    用imp命令导入dmp文件时提示以下错误: IMP-00058: 遇到 ORACLE 错误 12560 : ORA-12560: TNS: 协议适配器错误 : IMP-00000: 未成功终止导入 : ...

  3. Python学习笔记(四十八)POP3收取邮件

    收取邮件就是编写一个MUA作为客户端,从MDA把邮件获取到用户的电脑或者手机上.收取邮件最常用的协议是POP协议,目前版本号是3,俗称POP3. Python内置一个poplib模块,实现了POP3协 ...

  4. C11线程管理:异步操作

    1.异步操作 C++11提供了异步操作相关的类,std::future.std::promise和std::package_task.std::future作为异步结果的传输通道,方便的获取线程函数的 ...

  5. javascript在IE下不能用 trim函数解决方法

    javascript 的trim 函数在firefox 下面使用没有问题 <script language="javascript"> var test1 = &quo ...

  6. [BZOJ2809&1455&1367]解题报告|可并堆

    其实非常好理解..就是可以可以合并起来的两个堆嘛>< 2809: [Apio2012]dispatching Description 在一个忍者的帮派里,一些忍者们被选中派遣给顾客,然后依 ...

  7. 数据库-SQLite

    技术博客http://www.cnblogs.com/ChenYilong/ 新浪微博http://weibo.com/luohanchenyilong  数据库-SQLite 技术博客http:// ...

  8. stanfordCorenlp在python3中的安装使用+词性学习

    1 安装 前言 Stanford CoreNLP的源代码是使用Java写的,提供了Server方式进行交互.stanfordcorenlp是一个对Stanford CoreNLP进行了封装的Pytho ...

  9. WordPress的SEO插件——WordPress SEO by yoast安装及使用

    插件:WordPress SEO by yoast 使用方法: 做好网站SEO一直是站长们的愿望,说简单也简单,但是说难也难,因为需要注意的地方太多,一个不小心被百度K了你都不知道怎么回事.这里和大家 ...

  10. ThinkPHP的运行流程-2

    Thinkphp为了提高编译的效率,第一次运行的时候thinkphp会把文件全部编译到temp目录下的~runtime.php文件,在第二次运行的时候会直接读取这个文件.所以我们在线下自己写代码测试的 ...