UNPKG

gnablib

Version:

A lean, zero dependency library to provide a useful base for your project.

2 lines 1.04 kB
/*! Copyright 2024-2025 the gnablib contributors MPL-1.1 */ import{asLE as t}from"../endian/platform.js";import{ContentError as r}from"../error/ContentError.js";import{countLeadZeros as e}from"../primitive/xtBit.js";import{sLen as s}from"../safe/safe.js";import{APrng32 as i}from"./APrng32.js";export class MiddleSquare extends i{constructor(t,r){super(t,r),this.safeBits=this.bitGen,this._div=Math.pow(10,t[1]/2),this._mod=Math.pow(10,t[1]),this.bitGen=32-e(this._mod-1)}trueSave(){const r=new Uint8Array(this._state.slice().buffer,0,5);return t.i32(r,0,1),r}rawNext(){return this._state[0]=Math.floor(this._state[0]*this._state[0]/this._div)%this._mod,this._state[0]}get[Symbol.toStringTag](){return"middlesquare"}static seed(t,e,s=!1){const i=Uint32Array.of(t,0);if(e||(e=1+(0|Math.log10(i[0]))),!(1&~e))throw new r("Must have an even number of digits");return i[1]=e,new MiddleSquare(i,s)}static restore(t,r=!1){s("state",t).exactly(5).throwNot();const e=Uint32Array.of(t[0]|t[1]<<8|t[2]<<16|t[3]<<24,t[4]);return new MiddleSquare(e,r)}}