Comparing version 1.7.0 to 2.0.0
@@ -24,10 +24,12 @@ function toUint32(num) { | ||
var mt = prev.slice(); | ||
for (var idx = 0; idx !== MersenneTwister.N; ++idx) { | ||
var x = (mt[idx] & MersenneTwister.MASK_UPPER) + (mt[(idx + 1) % MersenneTwister.N] & MersenneTwister.MASK_LOWER); | ||
var xA = x >>> 1; | ||
if (x & 1) { | ||
xA ^= MersenneTwister.A; | ||
} | ||
mt[idx] = mt[(idx + MersenneTwister.M) % MersenneTwister.N] ^ xA; | ||
for (var idx = 0; idx !== MersenneTwister.N - MersenneTwister.M; ++idx) { | ||
var y_1 = (mt[idx] & MersenneTwister.MASK_UPPER) + (mt[idx + 1] & MersenneTwister.MASK_LOWER); | ||
mt[idx] = mt[idx + MersenneTwister.M] ^ (y_1 >>> 1) ^ (-(y_1 & 1) & MersenneTwister.A); | ||
} | ||
for (var idx = MersenneTwister.N - MersenneTwister.M; idx !== MersenneTwister.N - 1; ++idx) { | ||
var y_2 = (mt[idx] & MersenneTwister.MASK_UPPER) + (mt[idx + 1] & MersenneTwister.MASK_LOWER); | ||
mt[idx] = mt[idx + MersenneTwister.M - MersenneTwister.N] ^ (y_2 >>> 1) ^ (-(y_2 & 1) & MersenneTwister.A); | ||
} | ||
var y = (mt[MersenneTwister.N - 1] & MersenneTwister.MASK_UPPER) + (mt[0] & MersenneTwister.MASK_LOWER); | ||
mt[MersenneTwister.N - 1] = mt[MersenneTwister.M - 1] ^ (y >>> 1) ^ (-(y & 1) & MersenneTwister.A); | ||
return mt; | ||
@@ -34,0 +36,0 @@ }; |
@@ -17,10 +17,30 @@ var XorShift128Plus = (function () { | ||
var a1 = this.s01 ^ ((this.s01 << 23) | (this.s00 >>> 9)); | ||
var b0 = a0 ^ this.s10 ^ ((a0 >>> 17) | (a1 << 15)) ^ ((this.s10 >>> 26) | (this.s11 << 6)); | ||
var b1 = a1 ^ this.s11 ^ (a1 >>> 17) ^ (this.s11 >>> 26); | ||
return [(b0 + this.s10) | 0, new XorShift128Plus(this.s11, this.s10, b1, b0)]; | ||
var b0 = a0 ^ this.s10 ^ ((a0 >>> 18) | (a1 << 14)) ^ ((this.s10 >>> 5) | (this.s11 << 27)); | ||
var b1 = a1 ^ this.s11 ^ (a1 >>> 18) ^ (this.s11 >>> 5); | ||
return [(this.s00 + this.s10) | 0, new XorShift128Plus(this.s11, this.s10, b1, b0)]; | ||
}; | ||
XorShift128Plus.prototype.jump = function () { | ||
var rngRunner = this; | ||
var ns01 = 0; | ||
var ns00 = 0; | ||
var ns11 = 0; | ||
var ns10 = 0; | ||
var jump = [0x635d2dff, 0x8a5cd789, 0x5c472f96, 0x121fd215]; | ||
for (var i = 0; i !== 4; ++i) { | ||
for (var mask = 1; mask; mask <<= 1) { | ||
if (jump[i] & mask) { | ||
ns01 ^= rngRunner.s01; | ||
ns00 ^= rngRunner.s00; | ||
ns11 ^= rngRunner.s11; | ||
ns10 ^= rngRunner.s10; | ||
} | ||
rngRunner = rngRunner.next()[1]; | ||
} | ||
} | ||
return new XorShift128Plus(ns01, ns00, ns11, ns10); | ||
}; | ||
return XorShift128Plus; | ||
}()); | ||
export var xorshift128plus = function (seed) { | ||
return new XorShift128Plus(-1, ~seed, 0, seed | 0); | ||
return new XorShift128Plus(-1, ~seed, seed | 0, 0); | ||
}; |
@@ -5,4 +5,5 @@ import { generateN, skipN } from './generator/RandomGenerator'; | ||
import { xorshift128plus } from './generator/XorShift'; | ||
import { xoroshiro128plus } from './generator/XoroShiro'; | ||
import { uniformBigIntDistribution } from './distribution/UniformBigIntDistribution'; | ||
import { uniformIntDistribution } from './distribution/UniformIntDistribution'; | ||
export { generateN, skipN, congruential, congruential32, mersenne, xorshift128plus, uniformBigIntDistribution, uniformIntDistribution }; | ||
export { generateN, skipN, congruential, congruential32, mersenne, xorshift128plus, xoroshiro128plus, uniformBigIntDistribution, uniformIntDistribution }; |
@@ -26,10 +26,12 @@ "use strict"; | ||
var mt = prev.slice(); | ||
for (var idx = 0; idx !== MersenneTwister.N; ++idx) { | ||
var x = (mt[idx] & MersenneTwister.MASK_UPPER) + (mt[(idx + 1) % MersenneTwister.N] & MersenneTwister.MASK_LOWER); | ||
var xA = x >>> 1; | ||
if (x & 1) { | ||
xA ^= MersenneTwister.A; | ||
} | ||
mt[idx] = mt[(idx + MersenneTwister.M) % MersenneTwister.N] ^ xA; | ||
for (var idx = 0; idx !== MersenneTwister.N - MersenneTwister.M; ++idx) { | ||
var y_1 = (mt[idx] & MersenneTwister.MASK_UPPER) + (mt[idx + 1] & MersenneTwister.MASK_LOWER); | ||
mt[idx] = mt[idx + MersenneTwister.M] ^ (y_1 >>> 1) ^ (-(y_1 & 1) & MersenneTwister.A); | ||
} | ||
for (var idx = MersenneTwister.N - MersenneTwister.M; idx !== MersenneTwister.N - 1; ++idx) { | ||
var y_2 = (mt[idx] & MersenneTwister.MASK_UPPER) + (mt[idx + 1] & MersenneTwister.MASK_LOWER); | ||
mt[idx] = mt[idx + MersenneTwister.M - MersenneTwister.N] ^ (y_2 >>> 1) ^ (-(y_2 & 1) & MersenneTwister.A); | ||
} | ||
var y = (mt[MersenneTwister.N - 1] & MersenneTwister.MASK_UPPER) + (mt[0] & MersenneTwister.MASK_LOWER); | ||
mt[MersenneTwister.N - 1] = mt[MersenneTwister.M - 1] ^ (y >>> 1) ^ (-(y & 1) & MersenneTwister.A); | ||
return mt; | ||
@@ -36,0 +38,0 @@ }; |
@@ -19,10 +19,30 @@ "use strict"; | ||
var a1 = this.s01 ^ ((this.s01 << 23) | (this.s00 >>> 9)); | ||
var b0 = a0 ^ this.s10 ^ ((a0 >>> 17) | (a1 << 15)) ^ ((this.s10 >>> 26) | (this.s11 << 6)); | ||
var b1 = a1 ^ this.s11 ^ (a1 >>> 17) ^ (this.s11 >>> 26); | ||
return [(b0 + this.s10) | 0, new XorShift128Plus(this.s11, this.s10, b1, b0)]; | ||
var b0 = a0 ^ this.s10 ^ ((a0 >>> 18) | (a1 << 14)) ^ ((this.s10 >>> 5) | (this.s11 << 27)); | ||
var b1 = a1 ^ this.s11 ^ (a1 >>> 18) ^ (this.s11 >>> 5); | ||
return [(this.s00 + this.s10) | 0, new XorShift128Plus(this.s11, this.s10, b1, b0)]; | ||
}; | ||
XorShift128Plus.prototype.jump = function () { | ||
var rngRunner = this; | ||
var ns01 = 0; | ||
var ns00 = 0; | ||
var ns11 = 0; | ||
var ns10 = 0; | ||
var jump = [0x635d2dff, 0x8a5cd789, 0x5c472f96, 0x121fd215]; | ||
for (var i = 0; i !== 4; ++i) { | ||
for (var mask = 1; mask; mask <<= 1) { | ||
if (jump[i] & mask) { | ||
ns01 ^= rngRunner.s01; | ||
ns00 ^= rngRunner.s00; | ||
ns11 ^= rngRunner.s11; | ||
ns10 ^= rngRunner.s10; | ||
} | ||
rngRunner = rngRunner.next()[1]; | ||
} | ||
} | ||
return new XorShift128Plus(ns01, ns00, ns11, ns10); | ||
}; | ||
return XorShift128Plus; | ||
}()); | ||
exports.xorshift128plus = function (seed) { | ||
return new XorShift128Plus(-1, ~seed, 0, seed | 0); | ||
return new XorShift128Plus(-1, ~seed, seed | 0, 0); | ||
}; |
@@ -13,2 +13,4 @@ "use strict"; | ||
exports.xorshift128plus = XorShift_1.xorshift128plus; | ||
var XoroShiro_1 = require("./generator/XoroShiro"); | ||
exports.xoroshiro128plus = XoroShiro_1.xoroshiro128plus; | ||
var UniformBigIntDistribution_1 = require("./distribution/UniformBigIntDistribution"); | ||
@@ -15,0 +17,0 @@ exports.uniformBigIntDistribution = UniformBigIntDistribution_1.uniformBigIntDistribution; |
export default interface RandomGenerator { | ||
next(): [number, RandomGenerator]; | ||
jump?(): RandomGenerator; | ||
min(): number; | ||
@@ -4,0 +5,0 @@ max(): number; |
@@ -5,5 +5,6 @@ import { RandomGenerator, generateN, skipN } from './generator/RandomGenerator'; | ||
import { xorshift128plus } from './generator/XorShift'; | ||
import { xoroshiro128plus } from './generator/XoroShiro'; | ||
import Distribution from './distribution/Distribution'; | ||
import { uniformBigIntDistribution } from './distribution/UniformBigIntDistribution'; | ||
import { uniformIntDistribution } from './distribution/UniformIntDistribution'; | ||
export { RandomGenerator, generateN, skipN, congruential, congruential32, mersenne, xorshift128plus, Distribution, uniformBigIntDistribution, uniformIntDistribution }; | ||
export { RandomGenerator, generateN, skipN, congruential, congruential32, mersenne, xorshift128plus, xoroshiro128plus, Distribution, uniformBigIntDistribution, uniformIntDistribution }; |
export default interface RandomGenerator { | ||
next(): [number, RandomGenerator]; | ||
jump?(): RandomGenerator; | ||
min(): number; | ||
@@ -4,0 +5,0 @@ max(): number; |
@@ -5,5 +5,6 @@ import { RandomGenerator, generateN, skipN } from './generator/RandomGenerator'; | ||
import { xorshift128plus } from './generator/XorShift'; | ||
import { xoroshiro128plus } from './generator/XoroShiro'; | ||
import Distribution from './distribution/Distribution'; | ||
import { uniformBigIntDistribution } from './distribution/UniformBigIntDistribution'; | ||
import { uniformIntDistribution } from './distribution/UniformIntDistribution'; | ||
export { RandomGenerator, generateN, skipN, congruential, congruential32, mersenne, xorshift128plus, Distribution, uniformBigIntDistribution, uniformIntDistribution }; | ||
export { RandomGenerator, generateN, skipN, congruential, congruential32, mersenne, xorshift128plus, xoroshiro128plus, Distribution, uniformBigIntDistribution, uniformIntDistribution }; |
{ | ||
"name": "pure-rand", | ||
"version": "1.7.0", | ||
"version": "2.0.0", | ||
"description": " Pure random number generator written in TypeScript", | ||
@@ -21,4 +21,4 @@ "main": "lib/pure-rand.js", | ||
"build:esm": "tsc --module es2015 --outDir lib/esm --moduleResolution node", | ||
"test": "nyc mocha \"test/**/*.spec.ts\"", | ||
"coverage": "nyc report --reporter=text-lcov | coveralls", | ||
"test": "jest --config jest.config.js --coverage", | ||
"coverage": "cat ./coverage/lcov.info | coveralls", | ||
"build:bench:old": "tsc --target es6", | ||
@@ -40,14 +40,14 @@ "build:bench:new": "tsc --target es6 --outDir lib-new/", | ||
"devDependencies": { | ||
"@types/mocha": "^5.2.5", | ||
"@types/node": "^10.12.15", | ||
"@types/jest": "^24.9.0", | ||
"@types/node": "^13.1.8", | ||
"benchmark": "^2.1.4", | ||
"coveralls": "^3.0.2", | ||
"fast-check": "^1.9.2", | ||
"glob": "^7.1.3", | ||
"mocha": "^5.2.0", | ||
"nyc": "^13.0.1", | ||
"coveralls": "^3.0.9", | ||
"fast-check": "^1.21.0", | ||
"glob": "^7.1.6", | ||
"jest": "^24.9.0", | ||
"prettier": "1.16.4", | ||
"source-map-support": "^0.5.9", | ||
"ts-node": "^8.0.2", | ||
"typescript": "^3.2.2" | ||
"source-map-support": "^0.5.16", | ||
"ts-jest": "^24.3.0", | ||
"ts-node": "^8.6.2", | ||
"typescript": "^3.7.5" | ||
}, | ||
@@ -54,0 +54,0 @@ "keywords": [ |
// @ts-check | ||
// This file is a sample snippet to run benchmark accross versions | ||
// This file is a sample snippet to run benchmark across versions | ||
// Run it: | ||
@@ -4,0 +4,0 @@ // $: npm run build:bench:old |
@@ -78,2 +78,11 @@ # pure-rand | ||
const [nNoDistributionInstance, gen4] = prand.uniformIntDistribution(0, 9, gen3); | ||
// Some generators come with built-in jump | ||
// jump provides the ability to skip a very large number of intermediate values | ||
// Calling jump is recommended whenever you want to build non-overlapping subsequences | ||
const gen4 = prand.xoroshiro128plus(seed); | ||
const offsetGen4 = gen4.jump(); | ||
// In the case of: | ||
// - xoroshiro128plus - jump is equivalent to 2^64 calls to next | ||
// - xorshift128plus - jump is equivalent to 2^64 calls to next | ||
``` | ||
@@ -98,2 +107,3 @@ | ||
- `prand.xorshift128plus(seed: number)`: xorshift128+ generator whose values are within the range -0x80000000 to 0x7fffffff | ||
- `prand.xoroshiro128plus(seed: number)`: xoroshiro128+ generator whose values are within the range -0x80000000 to 0x7fffffff | ||
- `prand.mersenne(seed: number)`: Mersenne Twister generator whose values are within the range 0 to 0xffffffff | ||
@@ -100,0 +110,0 @@ - `prand.congruential(seed: number)`: Linear Congruential generator whose values are within the range 0 to 0x7fff |
Sorry, the diff of this file is not supported yet
47956
55
1015
124