pqp
Version:
NodeJS version of libPQP
68 lines (56 loc) • 2.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
exports.zeroArray = zeroArray;
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// from MDN(https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/random)
function getRandomInt(min, max) {
min = Math.ceil(min);
max = Math.floor(max);
return Math.floor(Math.random() * (max - min)) + min; //The maximum is exclusive and the minimum is inclusive
}
// fill array of `size` with `0`
function zeroArray(size) {
var result = new Float32Array(size);
result.fill(0);
return result;
}
var RandomGenerator = function () {
function RandomGenerator() {
_classCallCheck(this, RandomGenerator);
}
_createClass(RandomGenerator, [{
key: "get_random_vector",
value: function get_random_vector(size) {
var result = new Float32Array(size);
for (var i = 0; i < size; i++) {
result[i] = getRandomInt(0, 2);
}return result;
}
}, {
key: "get_random_weight_vector",
value: function get_random_weight_vector(length, weight) {
var random_indices = new Set();
for (var _ = 0; _ < weight; ++_) {
random_indices.add(getRandomInt(0, length));
}
while (random_indices.size < weight) {
random_indices.add(getRandomInt(0, length));
}
var real = zeroArray(length);
random_indices.forEach(function (idx) {
return real[idx] = 1;
});
return real;
}
}, {
key: "flip_coin",
value: function flip_coin() {
return getRandomInt(0, 2);
}
}]);
return RandomGenerator;
}();
exports.default = RandomGenerator;