-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
38 lines (33 loc) · 1.05 KB
/
Copy pathlib.js
File metadata and controls
38 lines (33 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
var exp = exports = module.exports;
// 使用 source 的属性来扩展 target 的属性
function extend(target, source) {
if (source) {
for (var key in source) {
var val = source[key];
if (typeof val !== "undefined") {
target[key] = val;
}
}
}
return target;
}
// 获取一个概率值
// some = 1, all = 5, 获取一个1/5的概率真值
function probability(some, all) {
var tmp = Math.random();
return tmp < some / all;
}
// 生成平均分布的 [min, max] 区间的数字
function genRandomNum(min, max) {
return Math.floor(Math.random() * (max - min + 1)) + min;
// return Math.floor(Math.random() * (max - min)) + min; // [min, max) // 不包括max
// return Math.ceil(Math.random() * (max - min + 1)) + min; // (min, max] // 不包括min
}
// 判断是不是数组
function isArray(o) {
return Object.prototype.toString.call(o) === '[object Array]';
}
exp.extend = extend;
exp.probability = probability;
exp.genRandomNum = genRandomNum;
exp.isArray = isArray;