Select Git revision
Monster.Data.Transformer.html
random.mjs 1.98 KiB
/** nodejs doesnt support window.crypt */
import {expect} from "chai"
import {getGlobal} from "../../../../application/source/types/global.mjs";
import * as Crypto from "@peculiar/webcrypto";
import {random} from "../../../../application/source/math/random.mjs";
const global = getGlobal();
if (!global['crypto']) {
global['crypto'] = new Crypto.Crypto();
}
describe('Math', function () {
describe('.random()', function () {
it('a greater b should throw error ', function () {
expect(() => random(10, 2)).to.throw(Error);
});
it(' should return a number ', function () {
let r = random();
expect(r).that.is.a('number');
});
it(' should return a number ', function () {
expect(() => random(5, 6)).to.throw(Error);
});
it(' should throw Error we cannot generate numbers larger than 53 bits.', function () {
expect(() => random(-999999999, 99999999999999999999)).to.throw(Error);
});
[
[1, 100],
[500, 600],
[100, 1000]
].forEach(function (data) {
let a = data.shift()
let b = data.shift()
let r = random(a, b);
it(r + ' should return value between ' + a + ' ' + b, function () {
expect(r >= a).to.be.true;
expect(r <= b).to.be.true;
});
});
[
[1, 100],
[500, 600],
[100, 1000]
].forEach(function (data) {
let a = data.shift()
let b = data.shift()
let sum = 0;
const rounds = 500
for (let i = 0, max = rounds; i < max; i++) {
sum += random(a, b);
}
let avg = sum / rounds;
it(avg + ' should between ' + a + ' ' + b, function () {
expect(avg > a).to.be.true
expect(avg < b).to.be.true
})
})
})
})