pure-rand
Advanced tools
Comparing version 5.0.5 to 6.0.0
@@ -5,35 +5,9 @@ "use strict"; | ||
function unsafeUniformIntDistributionInternal(rangeSize, rng) { | ||
var MinRng = rng.min(); | ||
var NumValues = rng.max() - rng.min() + 1; | ||
if (rangeSize <= NumValues) { | ||
var nrng_1 = rng; | ||
var MaxAllowed = NumValues - (NumValues % rangeSize); | ||
while (true) { | ||
var out = nrng_1.unsafeNext(); | ||
var deltaV = out - MinRng; | ||
if (deltaV < MaxAllowed) { | ||
return deltaV % rangeSize; | ||
} | ||
} | ||
var MaxAllowed = rangeSize > 2 ? ~~(0x100000000 / rangeSize) * rangeSize : 0x100000000; | ||
var deltaV = rng.unsafeNext() + 0x80000000; | ||
while (deltaV >= MaxAllowed) { | ||
deltaV = rng.unsafeNext() + 0x80000000; | ||
} | ||
var FinalNumValues = NumValues * NumValues; | ||
var NumIterations = 2; | ||
while (FinalNumValues < rangeSize) { | ||
FinalNumValues *= NumValues; | ||
++NumIterations; | ||
} | ||
var MaxAcceptedRandom = rangeSize * Math.floor((1 * FinalNumValues) / rangeSize); | ||
var nrng = rng; | ||
while (true) { | ||
var value = 0; | ||
for (var num = 0; num !== NumIterations; ++num) { | ||
var out = nrng.unsafeNext(); | ||
value = NumValues * value + (out - MinRng); | ||
} | ||
if (value < MaxAcceptedRandom) { | ||
var inDiff = value - rangeSize * Math.floor((1 * value) / rangeSize); | ||
return inDiff; | ||
} | ||
} | ||
return deltaV % rangeSize; | ||
} | ||
exports.unsafeUniformIntDistributionInternal = unsafeUniformIntDistributionInternal; |
@@ -7,6 +7,6 @@ "use strict"; | ||
var diff = to - from + SBigInt(1); | ||
var MinRng = SBigInt(rng.min()); | ||
var NumValues = SBigInt(rng.max() - rng.min() + 1); | ||
var MinRng = SBigInt(-0x80000000); | ||
var NumValues = SBigInt(0x100000000); | ||
var FinalNumValues = NumValues; | ||
var NumIterations = SBigInt(1); | ||
var NumIterations = 1; | ||
while (FinalNumValues < diff) { | ||
@@ -19,3 +19,3 @@ FinalNumValues *= NumValues; | ||
var value = SBigInt(0); | ||
for (var num = SBigInt(0); num !== NumIterations; ++num) { | ||
for (var num = 0; num !== NumIterations; ++num) { | ||
var out = rng.unsafeNext(); | ||
@@ -22,0 +22,0 @@ value = NumValues * value + (SBigInt(out) - MinRng); |
export function unsafeUniformIntDistributionInternal(rangeSize, rng) { | ||
var MinRng = rng.min(); | ||
var NumValues = rng.max() - rng.min() + 1; | ||
if (rangeSize <= NumValues) { | ||
var nrng_1 = rng; | ||
var MaxAllowed = NumValues - (NumValues % rangeSize); | ||
while (true) { | ||
var out = nrng_1.unsafeNext(); | ||
var deltaV = out - MinRng; | ||
if (deltaV < MaxAllowed) { | ||
return deltaV % rangeSize; | ||
} | ||
} | ||
var MaxAllowed = rangeSize > 2 ? ~~(0x100000000 / rangeSize) * rangeSize : 0x100000000; | ||
var deltaV = rng.unsafeNext() + 0x80000000; | ||
while (deltaV >= MaxAllowed) { | ||
deltaV = rng.unsafeNext() + 0x80000000; | ||
} | ||
var FinalNumValues = NumValues * NumValues; | ||
var NumIterations = 2; | ||
while (FinalNumValues < rangeSize) { | ||
FinalNumValues *= NumValues; | ||
++NumIterations; | ||
} | ||
var MaxAcceptedRandom = rangeSize * Math.floor((1 * FinalNumValues) / rangeSize); | ||
var nrng = rng; | ||
while (true) { | ||
var value = 0; | ||
for (var num = 0; num !== NumIterations; ++num) { | ||
var out = nrng.unsafeNext(); | ||
value = NumValues * value + (out - MinRng); | ||
} | ||
if (value < MaxAcceptedRandom) { | ||
var inDiff = value - rangeSize * Math.floor((1 * value) / rangeSize); | ||
return inDiff; | ||
} | ||
} | ||
return deltaV % rangeSize; | ||
} |
var SBigInt = typeof BigInt !== 'undefined' ? BigInt : undefined; | ||
export function unsafeUniformBigIntDistribution(from, to, rng) { | ||
var diff = to - from + SBigInt(1); | ||
var MinRng = SBigInt(rng.min()); | ||
var NumValues = SBigInt(rng.max() - rng.min() + 1); | ||
var MinRng = SBigInt(-0x80000000); | ||
var NumValues = SBigInt(0x100000000); | ||
var FinalNumValues = NumValues; | ||
var NumIterations = SBigInt(1); | ||
var NumIterations = 1; | ||
while (FinalNumValues < diff) { | ||
@@ -15,3 +15,3 @@ FinalNumValues *= NumValues; | ||
var value = SBigInt(0); | ||
for (var num = SBigInt(0); num !== NumIterations; ++num) { | ||
for (var num = 0; num !== NumIterations; ++num) { | ||
var out = rng.unsafeNext(); | ||
@@ -18,0 +18,0 @@ value = NumValues * value + (SBigInt(out) - MinRng); |
@@ -11,28 +11,2 @@ var MULTIPLIER = 0x000343fd; | ||
}; | ||
var LinearCongruential = (function () { | ||
function LinearCongruential(seed) { | ||
this.seed = seed; | ||
} | ||
LinearCongruential.prototype.min = function () { | ||
return LinearCongruential.min; | ||
}; | ||
LinearCongruential.prototype.max = function () { | ||
return LinearCongruential.max; | ||
}; | ||
LinearCongruential.prototype.clone = function () { | ||
return new LinearCongruential(this.seed); | ||
}; | ||
LinearCongruential.prototype.next = function () { | ||
var nextRng = new LinearCongruential(this.seed); | ||
var out = nextRng.unsafeNext(); | ||
return [out, nextRng]; | ||
}; | ||
LinearCongruential.prototype.unsafeNext = function () { | ||
this.seed = computeNextSeed(this.seed); | ||
return computeValueFromNextSeed(this.seed); | ||
}; | ||
LinearCongruential.min = 0; | ||
LinearCongruential.max = Math.pow(2, 15) - 1; | ||
return LinearCongruential; | ||
}()); | ||
var LinearCongruential32 = (function () { | ||
@@ -42,8 +16,2 @@ function LinearCongruential32(seed) { | ||
} | ||
LinearCongruential32.prototype.min = function () { | ||
return LinearCongruential32.min; | ||
}; | ||
LinearCongruential32.prototype.max = function () { | ||
return LinearCongruential32.max; | ||
}; | ||
LinearCongruential32.prototype.clone = function () { | ||
@@ -65,13 +33,8 @@ return new LinearCongruential32(this.seed); | ||
var vnext = v3 + ((v2 + (v1 << 15)) << 15); | ||
return ((vnext + 0x80000000) | 0) + 0x80000000; | ||
return vnext | 0; | ||
}; | ||
LinearCongruential32.min = 0; | ||
LinearCongruential32.max = 0xffffffff; | ||
return LinearCongruential32; | ||
}()); | ||
export var congruential = function (seed) { | ||
return new LinearCongruential(seed); | ||
}; | ||
export var congruential32 = function (seed) { | ||
return new LinearCongruential32(seed); | ||
}; |
@@ -32,8 +32,2 @@ var MersenneTwister = (function () { | ||
}; | ||
MersenneTwister.prototype.min = function () { | ||
return MersenneTwister.min; | ||
}; | ||
MersenneTwister.prototype.max = function () { | ||
return MersenneTwister.max; | ||
}; | ||
MersenneTwister.prototype.clone = function () { | ||
@@ -57,6 +51,4 @@ return new MersenneTwister(this.states, this.index); | ||
} | ||
return y >>> 0; | ||
return y; | ||
}; | ||
MersenneTwister.min = 0; | ||
MersenneTwister.max = 0xffffffff; | ||
MersenneTwister.N = 624; | ||
@@ -63,0 +55,0 @@ MersenneTwister.M = 397; |
@@ -8,8 +8,2 @@ var XoroShiro128Plus = (function () { | ||
} | ||
XoroShiro128Plus.prototype.min = function () { | ||
return -0x80000000; | ||
}; | ||
XoroShiro128Plus.prototype.max = function () { | ||
return 0x7fffffff; | ||
}; | ||
XoroShiro128Plus.prototype.clone = function () { | ||
@@ -16,0 +10,0 @@ return new XoroShiro128Plus(this.s01, this.s00, this.s11, this.s10); |
@@ -8,8 +8,2 @@ var XorShift128Plus = (function () { | ||
} | ||
XorShift128Plus.prototype.min = function () { | ||
return -0x80000000; | ||
}; | ||
XorShift128Plus.prototype.max = function () { | ||
return 0x7fffffff; | ||
}; | ||
XorShift128Plus.prototype.clone = function () { | ||
@@ -16,0 +10,0 @@ return new XorShift128Plus(this.s01, this.s00, this.s11, this.s10); |
{ | ||
"type": "module" | ||
} | ||
"type": "module" | ||
} |
import { generateN, skipN, unsafeGenerateN, unsafeSkipN } from './generator/RandomGenerator.js'; | ||
import { congruential, congruential32 } from './generator/LinearCongruential.js'; | ||
import { congruential32 } from './generator/LinearCongruential.js'; | ||
import mersenne from './generator/MersenneTwister.js'; | ||
@@ -13,4 +13,4 @@ import { xorshift128plus } from './generator/XorShift.js'; | ||
var __type = 'module'; | ||
var __version = '5.0.5'; | ||
var __commitHash = '8b812d9651b4a18e9115aa13d84fe1b5eb9b068d'; | ||
export { __type, __version, __commitHash, generateN, skipN, unsafeGenerateN, unsafeSkipN, congruential, congruential32, mersenne, xorshift128plus, xoroshiro128plus, uniformArrayIntDistribution, uniformBigIntDistribution, uniformIntDistribution, unsafeUniformArrayIntDistribution, unsafeUniformBigIntDistribution, unsafeUniformIntDistribution, }; | ||
var __version = '6.0.0'; | ||
var __commitHash = '5ea165aaca006e5d548883ccce3ca222b15b6553'; | ||
export { __type, __version, __commitHash, generateN, skipN, unsafeGenerateN, unsafeSkipN, congruential32, mersenne, xorshift128plus, xoroshiro128plus, uniformArrayIntDistribution, uniformBigIntDistribution, uniformIntDistribution, unsafeUniformArrayIntDistribution, unsafeUniformBigIntDistribution, unsafeUniformIntDistribution, }; |
"use strict"; | ||
exports.__esModule = true; | ||
exports.congruential32 = exports.congruential = void 0; | ||
exports.congruential32 = void 0; | ||
var MULTIPLIER = 0x000343fd; | ||
@@ -14,28 +14,2 @@ var INCREMENT = 0x00269ec3; | ||
}; | ||
var LinearCongruential = (function () { | ||
function LinearCongruential(seed) { | ||
this.seed = seed; | ||
} | ||
LinearCongruential.prototype.min = function () { | ||
return LinearCongruential.min; | ||
}; | ||
LinearCongruential.prototype.max = function () { | ||
return LinearCongruential.max; | ||
}; | ||
LinearCongruential.prototype.clone = function () { | ||
return new LinearCongruential(this.seed); | ||
}; | ||
LinearCongruential.prototype.next = function () { | ||
var nextRng = new LinearCongruential(this.seed); | ||
var out = nextRng.unsafeNext(); | ||
return [out, nextRng]; | ||
}; | ||
LinearCongruential.prototype.unsafeNext = function () { | ||
this.seed = computeNextSeed(this.seed); | ||
return computeValueFromNextSeed(this.seed); | ||
}; | ||
LinearCongruential.min = 0; | ||
LinearCongruential.max = Math.pow(2, 15) - 1; | ||
return LinearCongruential; | ||
}()); | ||
var LinearCongruential32 = (function () { | ||
@@ -45,8 +19,2 @@ function LinearCongruential32(seed) { | ||
} | ||
LinearCongruential32.prototype.min = function () { | ||
return LinearCongruential32.min; | ||
}; | ||
LinearCongruential32.prototype.max = function () { | ||
return LinearCongruential32.max; | ||
}; | ||
LinearCongruential32.prototype.clone = function () { | ||
@@ -68,12 +36,6 @@ return new LinearCongruential32(this.seed); | ||
var vnext = v3 + ((v2 + (v1 << 15)) << 15); | ||
return ((vnext + 0x80000000) | 0) + 0x80000000; | ||
return vnext | 0; | ||
}; | ||
LinearCongruential32.min = 0; | ||
LinearCongruential32.max = 0xffffffff; | ||
return LinearCongruential32; | ||
}()); | ||
var congruential = function (seed) { | ||
return new LinearCongruential(seed); | ||
}; | ||
exports.congruential = congruential; | ||
var congruential32 = function (seed) { | ||
@@ -80,0 +42,0 @@ return new LinearCongruential32(seed); |
@@ -34,8 +34,2 @@ "use strict"; | ||
}; | ||
MersenneTwister.prototype.min = function () { | ||
return MersenneTwister.min; | ||
}; | ||
MersenneTwister.prototype.max = function () { | ||
return MersenneTwister.max; | ||
}; | ||
MersenneTwister.prototype.clone = function () { | ||
@@ -59,6 +53,4 @@ return new MersenneTwister(this.states, this.index); | ||
} | ||
return y >>> 0; | ||
return y; | ||
}; | ||
MersenneTwister.min = 0; | ||
MersenneTwister.max = 0xffffffff; | ||
MersenneTwister.N = 624; | ||
@@ -65,0 +57,0 @@ MersenneTwister.M = 397; |
@@ -11,8 +11,2 @@ "use strict"; | ||
} | ||
XoroShiro128Plus.prototype.min = function () { | ||
return -0x80000000; | ||
}; | ||
XoroShiro128Plus.prototype.max = function () { | ||
return 0x7fffffff; | ||
}; | ||
XoroShiro128Plus.prototype.clone = function () { | ||
@@ -19,0 +13,0 @@ return new XoroShiro128Plus(this.s01, this.s00, this.s11, this.s10); |
@@ -11,8 +11,2 @@ "use strict"; | ||
} | ||
XorShift128Plus.prototype.min = function () { | ||
return -0x80000000; | ||
}; | ||
XorShift128Plus.prototype.max = function () { | ||
return 0x7fffffff; | ||
}; | ||
XorShift128Plus.prototype.clone = function () { | ||
@@ -19,0 +13,0 @@ return new XorShift128Plus(this.s01, this.s00, this.s11, this.s10); |
"use strict"; | ||
exports.__esModule = true; | ||
exports.unsafeUniformIntDistribution = exports.unsafeUniformBigIntDistribution = exports.unsafeUniformArrayIntDistribution = exports.uniformIntDistribution = exports.uniformBigIntDistribution = exports.uniformArrayIntDistribution = exports.xoroshiro128plus = exports.xorshift128plus = exports.mersenne = exports.congruential32 = exports.congruential = exports.unsafeSkipN = exports.unsafeGenerateN = exports.skipN = exports.generateN = exports.__commitHash = exports.__version = exports.__type = void 0; | ||
exports.unsafeUniformIntDistribution = exports.unsafeUniformBigIntDistribution = exports.unsafeUniformArrayIntDistribution = exports.uniformIntDistribution = exports.uniformBigIntDistribution = exports.uniformArrayIntDistribution = exports.xoroshiro128plus = exports.xorshift128plus = exports.mersenne = exports.congruential32 = exports.unsafeSkipN = exports.unsafeGenerateN = exports.skipN = exports.generateN = exports.__commitHash = exports.__version = exports.__type = void 0; | ||
var RandomGenerator_1 = require("./generator/RandomGenerator"); | ||
@@ -10,3 +10,2 @@ exports.generateN = RandomGenerator_1.generateN; | ||
var LinearCongruential_1 = require("./generator/LinearCongruential"); | ||
exports.congruential = LinearCongruential_1.congruential; | ||
exports.congruential32 = LinearCongruential_1.congruential32; | ||
@@ -33,5 +32,5 @@ var MersenneTwister_1 = require("./generator/MersenneTwister"); | ||
exports.__type = __type; | ||
var __version = '5.0.5'; | ||
var __version = '6.0.0'; | ||
exports.__version = __version; | ||
var __commitHash = '8b812d9651b4a18e9115aa13d84fe1b5eb9b068d'; | ||
var __commitHash = '5ea165aaca006e5d548883ccce3ca222b15b6553'; | ||
exports.__commitHash = __commitHash; |
import { RandomGenerator } from './RandomGenerator'; | ||
export declare const congruential: (seed: number) => RandomGenerator; | ||
export declare const congruential32: (seed: number) => RandomGenerator; |
export interface RandomGenerator { | ||
min(): number; | ||
max(): number; | ||
clone(): RandomGenerator; | ||
@@ -5,0 +3,0 @@ next(): [number, RandomGenerator]; |
import { RandomGenerator, generateN, skipN, unsafeGenerateN, unsafeSkipN } from './generator/RandomGenerator'; | ||
import { congruential, congruential32 } from './generator/LinearCongruential'; | ||
import { congruential32 } from './generator/LinearCongruential'; | ||
import mersenne from './generator/MersenneTwister'; | ||
@@ -16,2 +16,2 @@ import { xorshift128plus } from './generator/XorShift'; | ||
declare const __commitHash: string; | ||
export { __type, __version, __commitHash, RandomGenerator, generateN, skipN, unsafeGenerateN, unsafeSkipN, congruential, congruential32, mersenne, xorshift128plus, xoroshiro128plus, Distribution, uniformArrayIntDistribution, uniformBigIntDistribution, uniformIntDistribution, unsafeUniformArrayIntDistribution, unsafeUniformBigIntDistribution, unsafeUniformIntDistribution, }; | ||
export { __type, __version, __commitHash, RandomGenerator, generateN, skipN, unsafeGenerateN, unsafeSkipN, congruential32, mersenne, xorshift128plus, xoroshiro128plus, Distribution, uniformArrayIntDistribution, uniformBigIntDistribution, uniformIntDistribution, unsafeUniformArrayIntDistribution, unsafeUniformBigIntDistribution, unsafeUniformIntDistribution, }; |
{ | ||
"name": "pure-rand", | ||
"version": "5.0.5", | ||
"version": "6.0.0", | ||
"description": " Pure random number generator written in TypeScript", | ||
@@ -21,6 +21,6 @@ "type": "commonjs", | ||
"sideEffects": false, | ||
"packageManager": "yarn@3.3.0", | ||
"packageManager": "yarn@3.3.1", | ||
"scripts": { | ||
"format:check": "prettier --list-different \"**/*.{js,ts}\"", | ||
"format": "prettier --write \"**/*.{js,ts}\"", | ||
"format:check": "prettier --list-different .", | ||
"format": "prettier --write .", | ||
"build": "tsc && tsc -p ./tsconfig.declaration.json", | ||
@@ -46,14 +46,14 @@ "build:esm": "tsc --module es2015 --outDir lib/esm --moduleResolution node && cp package.esm-template.json lib/esm/package.json", | ||
"devDependencies": { | ||
"@types/jest": "^27.0.1", | ||
"@types/node": "^18.6.3", | ||
"@types/jest": "^27.5.2", | ||
"@types/node": "^18.11.18", | ||
"cross-env": "^7.0.3", | ||
"fast-check": "^3.1.1", | ||
"jest": "^26.1.0", | ||
"prettier": "2.7.1", | ||
"replace-in-file": "^6.1.0", | ||
"source-map-support": "^0.5.16", | ||
"fast-check": "^3.5.0", | ||
"jest": "^26.6.3", | ||
"prettier": "2.8.2", | ||
"replace-in-file": "^6.3.5", | ||
"source-map-support": "^0.5.21", | ||
"tinybench": "^2.3.1", | ||
"ts-jest": "^26.1.2", | ||
"ts-node": "^10.0.0", | ||
"typescript": "^4.0.2" | ||
"ts-jest": "^26.5.6", | ||
"ts-node": "^10.9.1", | ||
"typescript": "^4.9.4" | ||
}, | ||
@@ -60,0 +60,0 @@ "keywords": [ |
# pure-rand | ||
#### Pure random number generator written in TypeScript | ||
@@ -18,3 +19,3 @@ | ||
Unlike classical random number generators, `pure-rand` comes with a set of *pure* and *seeded* generators (implementing the interface [RandomGenerator](https://github.com/dubzzz/pure-rand/blob/main/src/generator/RandomGenerator.ts)). | ||
Unlike classical random number generators, `pure-rand` comes with a set of _pure_ and _seeded_ generators (implementing the interface [RandomGenerator](https://github.com/dubzzz/pure-rand/blob/main/src/generator/RandomGenerator.ts)). | ||
Each time a call to `.next()` method is done, the generator provides both the generated value and the next generator. | ||
@@ -30,4 +31,4 @@ | ||
<script type="module"> | ||
import * as prand from "https://unpkg.com/pure-rand/lib/esm/pure-rand.js"; | ||
// prand is now available | ||
import * as prand from 'https://unpkg.com/pure-rand/lib/esm/pure-rand.js'; | ||
// prand is now available | ||
</script> | ||
@@ -40,4 +41,4 @@ ``` | ||
<script type="module"> | ||
import * as prand from "https://unpkg.com/pure-rand@1.2.0/lib/esm/pure-rand.js"; | ||
// prand is now available | ||
import * as prand from 'https://unpkg.com/pure-rand@1.2.0/lib/esm/pure-rand.js'; | ||
// prand is now available | ||
</script> | ||
@@ -49,3 +50,3 @@ ``` | ||
```javascript | ||
import prand from 'pure-rand' | ||
import prand from 'pure-rand'; | ||
@@ -109,2 +110,3 @@ const seed = 42; | ||
The following generators are available: | ||
- `prand.xorshift128plus(seed: number)`: xorshift128+ generator whose values are within the range -0x80000000 to 0x7fffffff | ||
@@ -117,2 +119,3 @@ - `prand.xoroshiro128plus(seed: number)`: xoroshiro128+ generator whose values are within the range -0x80000000 to 0x7fffffff | ||
Some helpers are also provided in order to ease the use of `RandomGenerator` instances: | ||
- `prand.generateN(rng: RandomGenerator, num: number): [number[], RandomGenerator]`: generates `num` random values using `rng` and return the next `RandomGenerator` | ||
@@ -126,2 +129,3 @@ - `prand.skipN(rng: RandomGenerator, num: number): RandomGenerator`: skips `num` random values and return the next `RandomGenerator` | ||
For the moment, available `Distribution` are: | ||
- `prand.uniformIntDistribution(from: number, to: number): Distribution<number>` | ||
@@ -128,0 +132,0 @@ - `prand.uniformBigIntDistribution(from: bigint, to: bigint): Distribution<bigint>`\* |
131
60238
1228