| //#region src/distribution/uniformBigInt.ts | |
| const SBigInt = BigInt; | |
| const NumValues = 4294967296n; | |
| /** | |
| * Uniformly generate random bigint values between `from` (included) and `to` (included) | |
| * | |
| * @param rng - Instance of RandomGenerator to extract random values from | |
| * @param from - Lower bound of the range (included) | |
| * @param to - Upper bound of the range (included) | |
| * | |
| * @public | |
| */ | |
| function uniformBigInt(rng, from, to) { | |
| const diff = to - from + 1n; | |
| let FinalNumValues = NumValues; | |
| let NumIterations = 1; | |
| while (FinalNumValues < diff) { | |
| FinalNumValues <<= 32n; | |
| ++NumIterations; | |
| } | |
| let value = generateNext(NumIterations, rng); | |
| if (value < diff) return value + from; | |
| if (value + diff < FinalNumValues) return value % diff + from; | |
| const MaxAcceptedRandom = FinalNumValues - FinalNumValues % diff; | |
| while (value >= MaxAcceptedRandom) value = generateNext(NumIterations, rng); | |
| return value % diff + from; | |
| } | |
| function generateNext(NumIterations, rng) { | |
| let value = SBigInt(rng.next() + 2147483648); | |
| for (let num = 1; num < NumIterations; ++num) { | |
| const out = rng.next(); | |
| value = (value << 32n) + SBigInt(out + 2147483648); | |
| } | |
| return value; | |
| } | |
| //#endregion | |
| export { uniformBigInt }; | |
Xet Storage Details
- Size:
- 1.2 kB
- Xet hash:
- a81f3093a8e939f5969f6c37c22dade97f8d5011f8a683c3558196384e843c22
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.