1
1
import assert from 'assert'
2
2
import { filter , times } from 'lodash'
3
3
import math from '../../../../src/bundleAny'
4
+ import { flatten } from '../../../../src/utils/array'
4
5
5
6
const math2 = math . create ( { randomSeed : 'test2' } )
6
7
const pickRandom = math2 . pickRandom
@@ -10,12 +11,6 @@ describe('pickRandom', function () {
10
11
assert . strictEqual ( typeof math . pickRandom , 'function' )
11
12
} )
12
13
13
- it ( 'should throw an error when providing a multi dimensional matrix' , function ( ) {
14
- assert . throws ( function ( ) {
15
- pickRandom ( math . matrix ( [ [ 1 , 2 ] , [ 3 , 4 ] ] ) )
16
- } , / O n l y o n e d i m e n s i o n a l v e c t o r s s u p p o r t e d / )
17
- } )
18
-
19
14
it ( 'should throw an error if the length of the weights does not match the length of the possibles' , function ( ) {
20
15
const possibles = [ 11 , 22 , 33 , 44 , 55 ]
21
16
const weights = [ 1 , 5 , 2 , 4 ]
@@ -68,19 +63,19 @@ describe('pickRandom', function () {
68
63
const weights = [ 1 , 5 , 2 , 4 , 6 ]
69
64
const number = 5
70
65
71
- assert . strictEqual ( pickRandom ( possibles , number ) , possibles )
72
- assert . strictEqual ( pickRandom ( possibles , number , weights ) , possibles )
73
- assert . strictEqual ( pickRandom ( possibles , weights , number ) , possibles )
66
+ pickRandom ( possibles , number ) . forEach ( ( element , index ) => assert . strictEqual ( element , possibles [ index ] ) )
67
+ pickRandom ( possibles , number , weights ) . forEach ( ( element , index ) => assert . strictEqual ( element , possibles [ index ] ) )
68
+ pickRandom ( possibles , weights , number ) . forEach ( ( element , index ) => assert . strictEqual ( element , possibles [ index ] ) )
74
69
} )
75
70
76
71
it ( 'should return the given array if the given number is greater than its length' , function ( ) {
77
72
const possibles = [ 11 , 22 , 33 , 44 , 55 ]
78
73
const weights = [ 1 , 5 , 2 , 4 , 6 ]
79
74
const number = 6
80
75
81
- assert . strictEqual ( pickRandom ( possibles , number ) , possibles )
82
- assert . strictEqual ( pickRandom ( possibles , number , weights ) , possibles )
83
- assert . strictEqual ( pickRandom ( possibles , weights , number ) , possibles )
76
+ pickRandom ( possibles , number ) . forEach ( ( element , index ) => assert . strictEqual ( element , possibles [ index ] ) )
77
+ pickRandom ( possibles , number , weights ) . forEach ( ( element , index ) => assert . strictEqual ( element , possibles [ index ] ) )
78
+ pickRandom ( possibles , weights , number ) . forEach ( ( element , index ) => assert . strictEqual ( element , possibles [ index ] ) )
84
79
} )
85
80
86
81
it ( 'should return an empty array if the given number is 0' , function ( ) {
@@ -117,6 +112,30 @@ describe('pickRandom', function () {
117
112
assert . strictEqual ( pickRandom ( possibles , weights , number ) . length , number )
118
113
} )
119
114
115
+ it ( 'should pick a number from the given multi dimensional array following an uniform distribution' , function ( ) {
116
+ const possibles = [ [ 11 , 12 ] , [ 22 , 23 ] , [ 33 , 34 ] , [ 44 , 45 ] , [ 55 , 56 ] ]
117
+ const picked = [ ]
118
+
119
+ times ( 1000 , ( ) => picked . push ( pickRandom ( possibles ) ) )
120
+
121
+ flatten ( possibles ) . forEach ( possible => {
122
+ const count = filter ( flatten ( picked ) , val => val === possible ) . length
123
+ assert . strictEqual ( math . round ( count / picked . length , 1 ) , 0.1 )
124
+ } )
125
+ } )
126
+
127
+ it ( 'should pick a value from the given multi dimensional array following an uniform distribution' , function ( ) {
128
+ // just to be sure that works for any kind of array
129
+ const possibles = [ [ [ 11 ] , [ 12 ] ] , [ 'test' , 45 ] , 'another test' , 10 , false , [ 1.3 , 4.5 , true ] ]
130
+ const picked = [ ]
131
+
132
+ times ( 1000 , ( ) => picked . push ( pickRandom ( possibles ) ) )
133
+ flatten ( possibles ) . forEach ( possible => {
134
+ const count = filter ( picked , val => val === possible ) . length
135
+ assert . strictEqual ( math . round ( count / picked . length , 1 ) , 0.1 )
136
+ } )
137
+ } )
138
+
120
139
it ( 'should pick a value from the given array following an uniform distribution if only possibles are passed' , function ( ) {
121
140
const possibles = [ 11 , 22 , 33 , 44 , 55 ]
122
141
const picked = [ ]
0 commit comments