Skip to content

Commit 94972eb

Browse files
committed
add more math and random shortcuts
1 parent e62f66a commit 94972eb

File tree

5 files changed

+37
-6
lines changed

5 files changed

+37
-6
lines changed

array.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,16 @@ export const create = () => []
2020
* @return {Array<T>}
2121
*/
2222
export const copy = a => a.slice()
23+
24+
/**
25+
* Append elements from src to dest
26+
*
27+
* @template T
28+
* @param {Array<T>} dest
29+
* @param {Array<T>} src
30+
*/
31+
export const appendTo = (dest, src) => {
32+
for (let i = 0; i < src.length; i++) {
33+
dest.push(src[i])
34+
}
35+
}

math.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
export const floor = Math.floor
55
export const ceil = Math.ceil
66
export const abs = Math.abs
7-
7+
export const imul = Math.imul
88
export const round = Math.round
9+
export const log10 = Math.log10
10+
export const log2 = Math.log2
11+
export const log = Math.log
912

1013
/**
1114
* @function

prng.test.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { Xorshift32 } from './prng/Xorshift32.js'
88
import { Mt19937 } from './prng/Mt19937.js'
99
import * as dom from './dom.js'
1010
import { isBrowser, production } from './environment.js'
11+
import * as math from './math.js'
1112

1213
const genTestData = 5000
1314

@@ -31,8 +32,8 @@ const runGenTest = (tc, gen) => {
3132
}
3233
}
3334
t.info(`Generated ${head} heads and ${tail} tails.`)
34-
t.assert(tail >= Math.floor(genTestData * 0.45), 'Generated enough tails.')
35-
t.assert(head >= Math.floor(genTestData * 0.45), 'Generated enough heads.')
35+
t.assert(tail >= math.floor(genTestData * 0.45), 'Generated enough tails.')
36+
t.assert(head >= math.floor(genTestData * 0.45), 'Generated enough heads.')
3637
})
3738
t.group('int31 - integers average correctly', () => {
3839
let count = 0
@@ -44,7 +45,7 @@ const runGenTest = (tc, gen) => {
4445
const average = count / genTestData
4546
const expectedAverage = 100 / 2
4647
t.info(`Average is: ${average}. Expected average is ${expectedAverage}.`)
47-
t.assert(Math.abs(average - expectedAverage) <= 2, 'Expected average is at most 1 off.')
48+
t.assert(math.abs(average - expectedAverage) <= 2, 'Expected average is at most 1 off.')
4849
})
4950

5051
t.group('int32 - generates integer with 32 bits', () => {

prng/Mt19937.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as binary from '../binary.js'
2+
import * as math from '../math.js'
23

34
/**
45
* @module prng
@@ -48,7 +49,7 @@ export class Mt19937 {
4849
const state = new Uint32Array(N)
4950
state[0] = seed
5051
for (let i = 1; i < N; i++) {
51-
state[i] = (Math.imul(1812433253, (state[i - 1] ^ (state[i - 1] >>> 30))) + i) & binary.BITS32
52+
state[i] = (math.imul(1812433253, (state[i - 1] ^ (state[i - 1] >>> 30))) + i) & binary.BITS32
5253
}
5354
this._state = state
5455
this._i = 0

random.js

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55
*/
66
import * as env from './environment.js'
77
import * as binary from './binary.js'
8+
import * as math from './math.js'
9+
10+
/**
11+
* Basically Math.random. Returns a pseudo random number in [0,1).
12+
*/
13+
const rand = Math.random
814

915
/* istanbul ignore next */
1016
const uint32BrowserCrypto = () => {
@@ -14,7 +20,7 @@ const uint32BrowserCrypto = () => {
1420
}
1521

1622
/* istanbul ignore next */
17-
const uint32NoCrypto = () => Math.ceil((Math.random() * binary.BITS32) >>> 0)
23+
const uint32NoCrypto = () => math.ceil((rand() * binary.BITS32) >>> 0)
1824

1925
/**
2026
* @param {typeof import('crypto')} crypto
@@ -30,3 +36,10 @@ const uint32NodeCrypto = crypto => () => {
3036
export const uint32 = env.isBrowser
3137
? (typeof crypto === 'undefined' ? uint32NoCrypto : uint32BrowserCrypto)
3238
: uint32NodeCrypto(require('crypto'))
39+
40+
/**
41+
* @template T
42+
* @param {Array<T>} arr
43+
* @return {T}
44+
*/
45+
export const oneOf = arr => arr[math.floor(rand() * arr.length)]

0 commit comments

Comments
 (0)