Commit 952abef 1 parent e42eb90 commit 952abef Copy full SHA for 952abef
File tree 4 files changed +78
-11
lines changed
4 files changed +78
-11
lines changed Original file line number Diff line number Diff line change 183
183
return [ a , c ] ;
184
184
} ;
185
185
d3 . random = {
186
- normal : function ( mean , deviation ) {
187
- if ( arguments . length < 2 ) deviation = 1 ;
188
- if ( arguments . length < 1 ) mean = 0 ;
186
+ normal : function ( µ , σ ) {
187
+ var n = arguments . length ;
188
+ if ( n < 2 ) σ = 1 ;
189
+ if ( n < 1 ) µ = 0 ;
189
190
return function ( ) {
190
191
var x , y , r ;
191
192
do {
192
193
x = Math . random ( ) * 2 - 1 ;
193
194
y = Math . random ( ) * 2 - 1 ;
194
195
r = x * x + y * y ;
195
196
} while ( ! r || r > 1 ) ;
196
- return mean + deviation * x * Math . sqrt ( - 2 * Math . log ( r ) / r ) ;
197
+ return µ + σ * x * Math . sqrt ( - 2 * Math . log ( r ) / r ) ;
198
+ } ;
199
+ } ,
200
+ logNormal : function ( µ , σ ) {
201
+ var n = arguments . length ;
202
+ if ( n < 2 ) σ = 1 ;
203
+ if ( n < 1 ) µ = 0 ;
204
+ var random = d3 . random . normal ( ) ;
205
+ return function ( ) {
206
+ return Math . exp ( µ + σ * random ( ) ) ;
207
+ } ;
208
+ } ,
209
+ irwinHall : function ( m ) {
210
+ return function ( ) {
211
+ for ( var s = 0 , j = 0 ; j < m ; j ++ ) s += Math . random ( ) ;
212
+ return s / m ;
197
213
} ;
198
214
}
199
215
} ;
Original file line number Diff line number Diff line change 1
1
d3 . random = {
2
- normal : function ( mean , deviation ) {
3
- if ( arguments . length < 2 ) deviation = 1 ;
4
- if ( arguments . length < 1 ) mean = 0 ;
2
+ normal : function ( µ , σ ) {
3
+ var n = arguments . length ;
4
+ if ( n < 2 ) σ = 1 ;
5
+ if ( n < 1 ) µ = 0 ;
5
6
return function ( ) {
6
7
var x , y , r ;
7
8
do {
8
9
x = Math . random ( ) * 2 - 1 ;
9
10
y = Math . random ( ) * 2 - 1 ;
10
11
r = x * x + y * y ;
11
12
} while ( ! r || r > 1 ) ;
12
- return mean + deviation * x * Math . sqrt ( - 2 * Math . log ( r ) / r ) ;
13
+ return µ + σ * x * Math . sqrt ( - 2 * Math . log ( r ) / r ) ;
14
+ } ;
15
+ } ,
16
+ logNormal : function ( µ , σ ) {
17
+ var n = arguments . length ;
18
+ if ( n < 2 ) σ = 1 ;
19
+ if ( n < 1 ) µ = 0 ;
20
+ var random = d3 . random . normal ( ) ;
21
+ return function ( ) {
22
+ return Math . exp ( µ + σ * random ( ) ) ;
23
+ } ;
24
+ } ,
25
+ irwinHall : function ( m ) {
26
+ return function ( ) {
27
+ for ( var s = 0 , j = 0 ; j < m ; j ++ ) s += Math . random ( ) ;
28
+ return s / m ;
13
29
} ;
14
30
}
15
31
} ;
Original file line number Diff line number Diff line change
1
+ require ( "../env" ) ;
2
+
3
+ var vows = require ( "vows" ) ,
4
+ assert = require ( "assert" ) ;
5
+
6
+ var suite = vows . describe ( "d3.random" ) ;
7
+
8
+ suite . addBatch ( {
9
+ "normal" : {
10
+ "topic" : function ( ) {
11
+ return d3 . random . normal ( ) ;
12
+ } ,
13
+ "returns a number" : function ( random ) {
14
+ assert . typeOf ( random ( ) , "number" ) ;
15
+ }
16
+ } ,
17
+ "logNormal" : {
18
+ "topic" : function ( ) {
19
+ return d3 . random . logNormal ( ) ;
20
+ } ,
21
+ "returns a number" : function ( random ) {
22
+ assert . typeOf ( random ( ) , "number" ) ;
23
+ }
24
+ } ,
25
+ "irwinHall" : {
26
+ "topic" : function ( ) {
27
+ return d3 . random . irwinHall ( 10 ) ;
28
+ } ,
29
+ "returns a number" : function ( random ) {
30
+ assert . typeOf ( random ( ) , "number" ) ;
31
+ }
32
+ }
33
+ } ) ;
34
+
35
+ suite . export ( module ) ;
You can’t perform that action at this time.
0 commit comments