Skip to content

Commit

Permalink
add more math and random shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
dmonad committed Sep 17, 2019
1 parent e62f66a commit 94972eb
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 6 deletions.
13 changes: 13 additions & 0 deletions array.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,16 @@ export const create = () => []
* @return {Array<T>}
*/
export const copy = a => a.slice()

/**
* Append elements from src to dest
*
* @template T
* @param {Array<T>} dest
* @param {Array<T>} src
*/
export const appendTo = (dest, src) => {
for (let i = 0; i < src.length; i++) {
dest.push(src[i])
}
}
5 changes: 4 additions & 1 deletion math.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@
export const floor = Math.floor
export const ceil = Math.ceil
export const abs = Math.abs

export const imul = Math.imul
export const round = Math.round
export const log10 = Math.log10
export const log2 = Math.log2
export const log = Math.log

/**
* @function
Expand Down
7 changes: 4 additions & 3 deletions prng.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { Xorshift32 } from './prng/Xorshift32.js'
import { Mt19937 } from './prng/Mt19937.js'
import * as dom from './dom.js'
import { isBrowser, production } from './environment.js'
import * as math from './math.js'

const genTestData = 5000

Expand All @@ -31,8 +32,8 @@ const runGenTest = (tc, gen) => {
}
}
t.info(`Generated ${head} heads and ${tail} tails.`)
t.assert(tail >= Math.floor(genTestData * 0.45), 'Generated enough tails.')
t.assert(head >= Math.floor(genTestData * 0.45), 'Generated enough heads.')
t.assert(tail >= math.floor(genTestData * 0.45), 'Generated enough tails.')
t.assert(head >= math.floor(genTestData * 0.45), 'Generated enough heads.')
})
t.group('int31 - integers average correctly', () => {
let count = 0
Expand All @@ -44,7 +45,7 @@ const runGenTest = (tc, gen) => {
const average = count / genTestData
const expectedAverage = 100 / 2
t.info(`Average is: ${average}. Expected average is ${expectedAverage}.`)
t.assert(Math.abs(average - expectedAverage) <= 2, 'Expected average is at most 1 off.')
t.assert(math.abs(average - expectedAverage) <= 2, 'Expected average is at most 1 off.')
})

t.group('int32 - generates integer with 32 bits', () => {
Expand Down
3 changes: 2 additions & 1 deletion prng/Mt19937.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as binary from '../binary.js'
import * as math from '../math.js'

/**
* @module prng
Expand Down Expand Up @@ -48,7 +49,7 @@ export class Mt19937 {
const state = new Uint32Array(N)
state[0] = seed
for (let i = 1; i < N; i++) {
state[i] = (Math.imul(1812433253, (state[i - 1] ^ (state[i - 1] >>> 30))) + i) & binary.BITS32
state[i] = (math.imul(1812433253, (state[i - 1] ^ (state[i - 1] >>> 30))) + i) & binary.BITS32
}
this._state = state
this._i = 0
Expand Down
15 changes: 14 additions & 1 deletion random.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
*/
import * as env from './environment.js'
import * as binary from './binary.js'
import * as math from './math.js'

/**
* Basically Math.random. Returns a pseudo random number in [0,1).
*/
const rand = Math.random

/* istanbul ignore next */
const uint32BrowserCrypto = () => {
Expand All @@ -14,7 +20,7 @@ const uint32BrowserCrypto = () => {
}

/* istanbul ignore next */
const uint32NoCrypto = () => Math.ceil((Math.random() * binary.BITS32) >>> 0)
const uint32NoCrypto = () => math.ceil((rand() * binary.BITS32) >>> 0)

/**
* @param {typeof import('crypto')} crypto
Expand All @@ -30,3 +36,10 @@ const uint32NodeCrypto = crypto => () => {
export const uint32 = env.isBrowser
? (typeof crypto === 'undefined' ? uint32NoCrypto : uint32BrowserCrypto)
: uint32NodeCrypto(require('crypto'))

/**
* @template T
* @param {Array<T>} arr
* @return {T}
*/
export const oneOf = arr => arr[math.floor(rand() * arr.length)]

0 comments on commit 94972eb

Please sign in to comment.