forked from josdejong/mathjs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrandomInt.test.js
More file actions
117 lines (95 loc) · 3.22 KB
/
randomInt.test.js
File metadata and controls
117 lines (95 loc) · 3.22 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import assert from 'assert'
import math from '../../../../src/defaultInstance.js'
const math2 = math.create({ randomSeed: 'test' })
const randomInt = math2.randomInt
describe('randomInt', function () {
it('should have a function randomInt', function () {
assert.strictEqual(typeof math.randomInt, 'function')
})
it('should pick uniformly distributed integers in [min, max)', function () {
const picked = []
times(10000, function () {
picked.push(randomInt(-15, -5))
})
assertUniformDistributionInt(picked, -15, -5)
})
it('when called with no arguments, should flip a coin', function () {
const picked = Array.from({ length: 10000 }, () => randomInt())
assertUniformDistributionInt(picked, 0, 2)
})
it('should return a bigint given bigint limits', function () {
let picked = randomInt(1n, 7n)
assert.strictEqual(typeof picked, 'bigint')
assert(picked >= 1n)
assert(picked < 7n)
const wayBig = 10000000000000000n
picked = randomInt(wayBig, wayBig + 6n)
assert.strictEqual(typeof picked, 'bigint')
assert(picked >= wayBig)
assert(picked < wayBig + 6n)
picked = randomInt(1n, wayBig)
assert.strictEqual(typeof picked, 'bigint')
assert(picked >= 1n)
assert(picked < wayBig)
})
it('should pick uniformly distributed random array, with elements in [min, max)', function () {
const picked = []
const matrices = []
const size = [2, 3, 4]
times(1000, function () {
matrices.push(randomInt(size, -14.9, -2))
})
// Collect all values in one array
matrices.forEach(function (matrix) {
assert.deepStrictEqual(math.size(matrix), size)
math.forEach(matrix, function (val) {
picked.push(val)
})
})
assert.strictEqual(picked.length, 2 * 3 * 4 * 1000)
assertUniformDistributionInt(picked, -14.9, -2)
})
it('should throw an error if called with invalid arguments', function () {
assert.throws(function () {
randomInt(1, 2, [4, 8])
})
assert.throws(function () {
randomInt(1, 2, 3, 6)
})
})
it('should throw an error in case of wrong number of arguments', function () {
assert.throws(function () { randomInt([2, 3], 10, 100, 12) }, / Too many arguments/)
})
it('should LaTeX randomInt', function () {
const expression = math.parse('randomInt(0,100)')
assert.strictEqual(expression.toTex(), '\\mathrm{randomInt}\\left(0,100\\right)')
})
})
const assertUniformDistributionInt = function (values, min, max) {
const valuesRange = range(Math.floor(min), Math.floor(max))
let count
values.forEach(function (val) {
assert.ok(valuesRange.includes(val))
})
valuesRange.forEach(function (val) {
count = values.filter(function (testVal) { return testVal === val }).length
assertApproxEqual(count / values.length, 1 / valuesRange.length, 0.03)
})
}
const assertApproxEqual = function (testVal, val, tolerance) {
const diff = Math.abs(val - testVal)
if (diff > tolerance) assert.strictEqual(testVal, val)
else assert.ok(diff <= tolerance)
}
function times (n, callback) {
for (let i = 0; i < n; i++) {
callback()
}
}
function range (start, end) {
const array = []
for (let i = start; i < end; i++) {
array.push(i)
}
return array
}