Skip to content
Snippets Groups Projects
Select Git revision
  • 368a370349290e1770ba31cb07d7c8a471d138d9
  • master default protected
  • 1.31
  • 4.25.5
  • 4.25.4
  • 4.25.3
  • 4.25.2
  • 4.25.1
  • 4.25.0
  • 4.24.3
  • 4.24.2
  • 4.24.1
  • 4.24.0
  • 4.23.6
  • 4.23.5
  • 4.23.4
  • 4.23.3
  • 4.23.2
  • 4.23.1
  • 4.23.0
  • 4.22.3
  • 4.22.2
  • 4.22.1
23 results

Monster.DOM.Theme.html

Blame
  • random.mjs 2.43 KiB
    /** nodejs doesnt support window.crypt */
    import {expect} from "chai"
    
    describe('Math', function () {
    
        let random = () => {}
        
        before(function (done) {
    
            let promises = []
            if (!globalThis['crypto']) {
                promises.push(import("@peculiar/webcrypto").then(m => {
                    globalThis['crypto'] = new m.Crypto();
                    return true;
                }))
            }
    
            promises.push(import("../../../source/math/random.mjs").then(m => {
                random = m.random;
                return true;
            }))
    
    
            Promise.all(promises).then(() => {
                done()
            });
    
        });
    
        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);
            });
    
            it(' should throw Error we cannot generate numbers larger than 53 bits.', function () {
    
                [
                    [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
                    })
    
    
                })
    
            })
    
    
        })
    
    
    })