-
Notifications
You must be signed in to change notification settings - Fork 0
/
nsRandom.gs
84 lines (72 loc) · 2.36 KB
/
nsRandom.gs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
const nsRandom = (() => {
/**
* Generate an array of random numbers
* @param {number} min The lower limit
* @param {number} max The upper limit
* @param {integer} number The number of numbers
* @return {array} The array of random numbers
*/
const getRandomNumberList_ = (min, max, number) => {
try {
let arrNumbers = []
for (let i = 1; i < number + 1; i++) {
arrNumbers.push([getRandomNumber_(min, max)])
}
return arrNumbers
} catch (err) {
logIt({
level: "severe",
theMsg: "getRandomList nsRandom",
error: err
})
}
}
/**
* Generate an array of UNIQUE random numbers
* @param {number} min The lower limit
* @param {number} max The upper limit
* @param {integer} number The number of numbers
* @return {array} The array of random numbers
*/
const getRandomUniqueNumberList_ = (min, max, number) => {
try {
let arrNumbers = []
while (arrNumbers.length < number) {
let tmp = getRandomNumber_(min, max)
if (arrNumbers.indexOf(tmp) === -1) arrNumbers.push(tmp)
}
let newArr = []
arrNumbers.forEach(item => newArr.push([item]))
return newArr
} catch (err) {
logIt({
level: "severe",
theMsg: "getRandomUniqueList nsRandom",
error: err
})
}
}
/**
* Generates a randdom number between a min and max AND can include min and max
* @param {number} min the lower limit
* @param {number} max the upper limit
* @return {integer} The random number
*/
const getRandomNumber_ = (min, max) => {
return Math.floor(Math.random() * (max - min + 1) + min)
}
return {
getRandomNumber: getRandomNumber_,
getRandomNumberList: getRandomNumberList_,
getRandomUniqueNumberList: getRandomUniqueNumberList_
}
})()
const test = () => {
// for (var i = 1; i < 11; i++) {
// let rslt = nsRandom.getRandomNumber(5, 8)
// console.log(rslt);
// }
let rslt = nsRandom.getRandomNumberList(5, 9, 5)
let rslt2 = nsRandom.getRandomUniqueNumberList(5, 15, 5)
console.log(rslt2);
}