From 245d49c904fea6ffab9201cab58c4504b6ddfe3a Mon Sep 17 00:00:00 2001 From: Dan Crescimanno Date: Mon, 25 Apr 2022 23:14:01 -0700 Subject: [PATCH] feat: removed seedrandom in favor of inlining to help build on esm.sh --- package-lock.json | 1 - package.json | 1 - src/model_selection/KFold.test.ts | 2 +- src/neighbors/KdTree.ts | 2 +- src/neighbors/neighborhoodGenericTests.ts | 2 +- src/randUtils.ts | 77 ++++++++++++++++++++++- 6 files changed, 79 insertions(+), 6 deletions(-) diff --git a/package-lock.json b/package-lock.json index 3145c6ff..886a72ac 100644 --- a/package-lock.json +++ b/package-lock.json @@ -14,7 +14,6 @@ "@tensorflow/tfjs-node": "3.13.0", "lodash": "^4.17.21", "mathjs": "^10.0.0", - "seedrandom": "^3.0.5", "simple-statistics": "^7.7.0" }, "devDependencies": { diff --git a/package.json b/package.json index bb4ddc7c..53704d16 100644 --- a/package.json +++ b/package.json @@ -53,7 +53,6 @@ "@tensorflow/tfjs-node": "3.13.0", "lodash": "^4.17.21", "mathjs": "^10.0.0", - "seedrandom": "^3.0.5", "simple-statistics": "^7.7.0" }, "devDependencies": { diff --git a/src/model_selection/KFold.test.ts b/src/model_selection/KFold.test.ts index bb18ef14..625e0c0d 100644 --- a/src/model_selection/KFold.test.ts +++ b/src/model_selection/KFold.test.ts @@ -15,7 +15,7 @@ import * as fc from 'fast-check' import { KFold } from './KFold' -import { alea } from 'seedrandom' +import { alea } from '../randUtils' import '../jestTensorMatchers' import { tf } from '../shared/globals' type Tensor2D = tf.Tensor2D diff --git a/src/neighbors/KdTree.ts b/src/neighbors/KdTree.ts index 9a8750d5..2adefcd3 100644 --- a/src/neighbors/KdTree.ts +++ b/src/neighbors/KdTree.ts @@ -17,7 +17,7 @@ import { assert } from '../typesUtils' import { tf } from '../shared/globals' import { Neighborhood, NeighborhoodParams } from './Neighborhood' import * as randUtils from '../randUtils' -import { alea } from 'seedrandom' +import { alea } from '../randUtils' import { CappedMaxHeap } from './CappedMaxHeap' const child = (parent: number) => (parent << 1) + 1 diff --git a/src/neighbors/neighborhoodGenericTests.ts b/src/neighbors/neighborhoodGenericTests.ts index ce335682..275c8583 100644 --- a/src/neighbors/neighborhoodGenericTests.ts +++ b/src/neighbors/neighborhoodGenericTests.ts @@ -15,7 +15,7 @@ import * as fc from 'fast-check' import { tf } from '../shared/globals' -import { alea } from 'seedrandom' +import { alea } from '../randUtils' import { Neighborhood, NeighborhoodParams } from './Neighborhood' import { lhs, shuffle } from '../randUtils' import { minkowskiMetric } from './Metric' diff --git a/src/randUtils.ts b/src/randUtils.ts index 31b83fa4..4b0aebf6 100644 --- a/src/randUtils.ts +++ b/src/randUtils.ts @@ -12,8 +12,83 @@ * limitations under the License. * ========================================================================== */ +export function _prng_restore(prng: any, xg: any, opts: any) { + let state = opts && opts.state + if (state) { + if (typeof state == 'object') xg.copy(state, xg) + prng.state = () => xg.copy(xg, {}) + } +} + +export function alea(seed?: any, opts?: any) { + let xg = new AleaGen(seed) + + let prng = () => xg.next() + + _prng_restore(prng, xg, opts) + return prng +} + +class AleaGen { + c: number + s0: number + s1: number + s2: number + constructor(seed: any) { + if (seed == null) seed = +new Date() + + let n = 0xefc8249d -import { alea } from 'seedrandom' + // Apply the seeding algorithm from Baagoe. + this.c = 1 + this.s0 = mash(' ') + this.s1 = mash(' ') + this.s2 = mash(' ') + this.s0 -= mash(seed) + if (this.s0 < 0) { + this.s0 += 1 + } + this.s1 -= mash(seed) + if (this.s1 < 0) { + this.s1 += 1 + } + this.s2 -= mash(seed) + if (this.s2 < 0) { + this.s2 += 1 + } + + function mash(data: any) { + data = String(data) + for (let i = 0; i < data.length; i++) { + n += data.charCodeAt(i) + let h = 0.02519603282416938 * n + n = h >>> 0 + h -= n + h *= n + n = h >>> 0 + h -= n + n += h * 0x100000000 // 2^32 + } + return (n >>> 0) * 2.3283064365386963e-10 // 2^-32 + } + } + + next() { + let { c, s0, s1, s2 } = this + let t = 2091639 * s0 + c * 2.3283064365386963e-10 // 2^-32 + this.s0 = s1 + this.s1 = s2 + return (this.s2 = t - (this.c = t | 0)) + } + + copy(f: any, t: any) { + t.c = f.c + t.s0 = f.s0 + t.s1 = f.s1 + t.s2 = f.s2 + return t + } +} export type int = number pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy