UNPKG

luby

Version:

Generate fountain codes / erasure codes (Luby Transform, robust)

32 lines 1.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const shuffle_array_1 = __importDefault(require("shuffle-array")); function Random(seed) { this._seed = seed * 1000000 % 2147483647; if (this._seed <= 0) this._seed += 2147483646; } Random.prototype.next = function () { return (this._seed = (this._seed * 16807) % 2147483647); }; Random.prototype.nextFloat = function (opt_minOrMax, opt_max) { // We know that result of next() will be 1 to 2147483646 (inclusive). return (this.next() - 1) / 2147483646; }; function selectNeighbours(indexAsSeed, numberOfNeighborsToGet, K) { // pseudorandomly shuffle array, take desired number of elements const seededRandom = new Random(indexAsSeed); // console.log({ indexAsSeed, numberOfNeighborsToGet, K }); const nums = []; for (let i = 0; i < K; i++) { nums[i] = i; } shuffle_array_1.default(nums, { rng: seededRandom.nextFloat.bind(seededRandom) }); const res = nums.slice(0, numberOfNeighborsToGet); return res; } exports.default = selectNeighbours; //# sourceMappingURL=selectNeighbours.js.map