Skip to content

Commit

Permalink
pickRandom - flatten the array
Browse files Browse the repository at this point in the history
  • Loading branch information
KonradLinkowski committed Oct 3, 2020
1 parent ca05c25 commit a5cbb6a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 16 deletions.
3 changes: 2 additions & 1 deletion src/function/probability/pickRandom.js
@@ -1,6 +1,7 @@
import { factory } from '../../utils/factory'
import { isNumber } from '../../utils/is'
import { createRng } from './util/seededRNG'
import { flatten } from '../../utils/array'

const name = 'pickRandom'
const dependencies = ['typed', 'config', '?on']
Expand Down Expand Up @@ -75,7 +76,7 @@ export const createPickRandom = /* #__PURE__ */ factory(name, dependencies, ({ t
number = 1
}

possibles = possibles.valueOf() // get Array
possibles = flatten(possibles.valueOf()).valueOf() // get Array
if (weights) {
weights = weights.valueOf() // get Array
}
Expand Down
30 changes: 15 additions & 15 deletions test/unit-tests/function/probability/pickRandom.test.js
@@ -1,6 +1,7 @@
import assert from 'assert'
import { filter, times } from 'lodash'
import math from '../../../../src/bundleAny'
import { flatten } from '../../../../src/utils/array'

const math2 = math.create({ randomSeed: 'test2' })
const pickRandom = math2.pickRandom
Expand Down Expand Up @@ -62,19 +63,19 @@ describe('pickRandom', function () {
const weights = [1, 5, 2, 4, 6]
const number = 5

assert.strictEqual(pickRandom(possibles, number), possibles)
assert.strictEqual(pickRandom(possibles, number, weights), possibles)
assert.strictEqual(pickRandom(possibles, weights, number), possibles)
pickRandom(possibles, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, number, weights).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, weights, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
})

it('should return the given array if the given number is greater than its length', function () {
const possibles = [11, 22, 33, 44, 55]
const weights = [1, 5, 2, 4, 6]
const number = 6

assert.strictEqual(pickRandom(possibles, number), possibles)
assert.strictEqual(pickRandom(possibles, number, weights), possibles)
assert.strictEqual(pickRandom(possibles, weights, number), possibles)
pickRandom(possibles, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, number, weights).forEach((element, index) => assert.strictEqual(element, possibles[index]))
pickRandom(possibles, weights, number).forEach((element, index) => assert.strictEqual(element, possibles[index]))
})

it('should return an empty array if the given number is 0', function () {
Expand Down Expand Up @@ -111,28 +112,27 @@ describe('pickRandom', function () {
assert.strictEqual(pickRandom(possibles, weights, number).length, number)
})

it('should pick an array from the given multi dimensional array following an uniform distribution', function () {
it('should pick a number from the given multi dimensional array following an uniform distribution', function () {
const possibles = [[11, 12], [22, 23], [33, 34], [44, 45], [55, 56]]
const picked = []

times(1000, () => picked.push(pickRandom(possibles)))

possibles.forEach(possible => {
const count = filter(picked, val => val === possible).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
flatten(possibles).forEach(possible => {
const count = filter(flatten(picked), val => val === possible).length
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
})
})

it('should pick a value from the given array following an uniform distribution', function () {
it('should pick a value from the given multi dimensional array following an uniform distribution', function () {
// just to be sure that works for any kind of array
const possibles = [[[11], [12]], ['test', 45], 'another test', 10, false]
const possibles = [[[11], [12]], ['test', 45], 'another test', 10, false, [1.3, 4.5, true]]
const picked = []

times(1000, () => picked.push(pickRandom(possibles)))

possibles.forEach(possible => {
flatten(possibles).forEach(possible => {
const count = filter(picked, val => val === possible).length
assert.strictEqual(math.round(count / picked.length, 1), 0.2)
assert.strictEqual(math.round(count / picked.length, 1), 0.1)
})
})

Expand Down

0 comments on commit a5cbb6a

Please sign in to comment.