Skip to content

Commit 90d6849

Browse files
fix #2
1 parent 44e3985 commit 90d6849

File tree

14 files changed

+132
-109
lines changed

14 files changed

+132
-109
lines changed

js/dist/random.js

Lines changed: 49 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -3,87 +3,101 @@
33
'use strict';
44

55

6-
/* js/src/fisheryates.js */
6+
/* js/src/000-kernel */
7+
/* js/src/000-kernel/fisheryates.js */
78

89
/**
910
* Sample array using Fisher-Yates method.
1011
*/
1112

12-
var __fisheryates__ = function ( randint ) {
13+
var _fisheryates = function ( randint ) {
1314

14-
var fisheryates = function ( n, a, i, j ) {
15+
var fisheryates = function ( n , a , i , j ) {
1516

16-
var k, p, tmp;
17+
var k , p , tmp ;
1718

1819
// we will swap at most n elements
1920

20-
k = i + n;
21+
k = i + n ;
2122

2223
for ( ; i < k ; ++i ) {
2324

2425
// choose any index p in the remaining array
2526

26-
p = randint( i, j );
27+
p = randint( i , j ) ;
2728

2829

2930
// swap element at index p with first element in the array
3031

31-
tmp = a[i];
32-
a[i] = a[p];
33-
a[p] = tmp;
32+
tmp = a[i] ;
33+
a[i] = a[p] ;
34+
a[p] = tmp ;
3435

3536
}
3637

37-
};
38+
} ;
3839

39-
return fisheryates;
40+
return fisheryates ;
4041

41-
};
42+
} ;
4243

43-
exports.__fisheryates__ = __fisheryates__;
44-
exports.__sample__ = __fisheryates__;
44+
exports._fisheryates = _fisheryates ;
4545

46-
/* js/src/randfloat.js */
46+
/* js/src/000-kernel/shuffle.js */
47+
48+
/**
49+
* Shuffle array by sampling the complete array.
50+
*/
51+
52+
var _shuffle = function ( sample ) {
53+
54+
var shuffle = function ( a , i , j ) {
55+
sample( j - i , a , i , j ) ;
56+
} ;
57+
58+
return shuffle ;
59+
60+
} ;
61+
62+
exports._shuffle = _shuffle ;
63+
64+
/* js/src/001-api */
65+
/* js/src/001-api/000-randfloat.js */
4766

4867
/**
4968
* Returns a floating point number in interval [i, j[ (i included, j excluded)
5069
* according to a uniform distribution.
5170
*/
5271

53-
var randfloat = function ( i, j ) {
54-
return i + Math.random() * (j - i);
55-
};
72+
var randfloat = function ( i , j ) {
73+
return i + Math.random( ) * ( j - i ) ;
74+
} ;
5675

57-
exports.randfloat = randfloat;
76+
exports.randfloat = randfloat ;
5877

59-
/* js/src/randint.js */
78+
/* js/src/001-api/001-randint.js */
6079

6180
/**
6281
* Returns an integer in interval [i, j[ (i included, j excluded)
6382
* according to a uniform distribution.
6483
*/
6584

66-
var randint = function ( i, j ) {
67-
return i + Math.floor( Math.random() * (j - i) );
68-
};
85+
var randint = function ( i , j ) {
86+
return i + Math.floor( Math.random( ) * ( j - i ) ) ;
87+
} ;
6988

70-
exports.randint = randint;
89+
exports.randint = randint ;
7190

72-
/* js/src/shuffle.js */
91+
/* js/src/001-api/002-sample.js */
7392

74-
/**
75-
* Shuffle array by sampling the complete array.
76-
*/
93+
var sample = _fisheryates( randint ) ;
7794

78-
var __shuffle__ = function ( sample ) {
95+
exports.sample = sample ;
7996

80-
var shuffle = function ( a, i, j ) {
81-
sample(j - i, a, i, j);
82-
};
97+
/* js/src/001-api/003-shuffle.js */
8398

84-
return shuffle;
85-
};
99+
var shuffle = _shuffle( sample ) ;
86100

87-
exports.__shuffle__ = __shuffle__;
101+
exports.shuffle = shuffle ;
88102

89103
})(typeof exports === 'undefined' ? this['random'] = {} : exports);

js/dist/random.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/dist/random.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/src/000-kernel/fisheryates.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
2+
/**
3+
* Sample array using Fisher-Yates method.
4+
*/
5+
6+
var _fisheryates = function ( randint ) {
7+
8+
var fisheryates = function ( n , a , i , j ) {
9+
10+
var k , p , tmp ;
11+
12+
// we will swap at most n elements
13+
14+
k = i + n ;
15+
16+
for ( ; i < k ; ++i ) {
17+
18+
// choose any index p in the remaining array
19+
20+
p = randint( i , j ) ;
21+
22+
23+
// swap element at index p with first element in the array
24+
25+
tmp = a[i] ;
26+
a[i] = a[p] ;
27+
a[p] = tmp ;
28+
29+
}
30+
31+
} ;
32+
33+
return fisheryates ;
34+
35+
} ;
36+
37+
exports._fisheryates = _fisheryates ;

js/src/000-kernel/shuffle.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
/**
3+
* Shuffle array by sampling the complete array.
4+
*/
5+
6+
var _shuffle = function ( sample ) {
7+
8+
var shuffle = function ( a , i , j ) {
9+
sample( j - i , a , i , j ) ;
10+
} ;
11+
12+
return shuffle ;
13+
14+
} ;
15+
16+
exports._shuffle = _shuffle ;

js/src/randfloat.js renamed to js/src/001-api/000-randfloat.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
* according to a uniform distribution.
55
*/
66

7-
var randfloat = function ( i, j ) {
8-
return i + Math.random() * (j - i);
9-
};
7+
var randfloat = function ( i , j ) {
8+
return i + Math.random( ) * ( j - i ) ;
9+
} ;
1010

11-
exports.randfloat = randfloat;
11+
exports.randfloat = randfloat ;

js/src/001-api/001-randint.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
/**
3+
* Returns an integer in interval [i, j[ (i included, j excluded)
4+
* according to a uniform distribution.
5+
*/
6+
7+
var randint = function ( i , j ) {
8+
return i + Math.floor( Math.random( ) * ( j - i ) ) ;
9+
} ;
10+
11+
exports.randint = randint ;

js/src/001-api/002-sample.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
var sample = _fisheryates( randint ) ;
3+
4+
exports.sample = sample ;

js/src/001-api/003-shuffle.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
var shuffle = _shuffle( sample ) ;
3+
4+
exports.shuffle = shuffle ;

js/src/fisheryates.js

Lines changed: 0 additions & 38 deletions
This file was deleted.

0 commit comments

Comments
 (0)