forked from skale-me/skale
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gen_data.js
executable file
·49 lines (39 loc) · 1.04 KB
/
gen_data.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#!/usr/bin/env node
var fs = require('fs');
if (process.argv.length != 4) {
console.log('Usage: gen_data.js file size_in_Mo');
process.exit(1);
}
var file = process.argv[2];
var D = 16;
var maxSize = process.argv[3] * 1024 * 1024;
var rng = new Random();
var fd = fs.createWriteStream(file);
var fileSize = 0;
function writeChunk() {
var line = '';
for (var i = 0; i < 500; i++) {
line += 2 * Math.round(Math.abs(rng.randn(1))) - 1;
line += ' ' + rng.randn(D).join(' ') + '\n';
}
var lineSize = Buffer.byteLength(line, 'utf8');
if ((fileSize + lineSize) > maxSize) fd.end();
else fd.write(line, function() {fileSize += lineSize; writeChunk();});
}
writeChunk();
function Random(initSeed) {
this.seed = initSeed || 1;
this.next = function () {
var x = Math.sin(this.seed++) * 10000;
return (x - Math.floor(x)) * 2 - 1;
};
this.reset = function () {
this.seed = initSeed;
};
this.randn = function (N) {
var w = new Array(N);
for (var i = 0; i < N; i++)
w[i] = this.next();
return w;
};
}