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.06 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
const fs = require('fs');
if (process.argv.length !== 4) {
console.log('Usage: gen_data.js file size_in_Mo');
process.exit(1);
}
const file = process.argv[2];
const D = 16;
const maxSize = process.argv[3] * 1024 * 1024;
const rng = new Random();
const fd = fs.createWriteStream(file);
let fileSize = 0;
function writeChunk() {
let line = '';
for (let i = 0; i < 500; i++) {
line += 2 * Math.round(Math.abs(rng.randn(1))) - 1;
line += ' ' + rng.randn(D).join(' ') + '\n';
}
const 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 () {
const x = Math.sin(this.seed++) * 10000;
return (x - Math.floor(x)) * 2 - 1;
};
this.reset = function () {
this.seed = initSeed;
};
this.randn = function (N) {
const w = new Array(N);
for (let i = 0; i < N; i++)
w[i] = this.next();
return w;
};
}