gnablib
Version:
A lean, zero dependency library to provide a useful base for your project.
2 lines • 1.25 kB
JavaScript
/*! Copyright 2025 the gnablib contributors MPL-1.1 */
import{BitWriter as t}from"../primitive/BitWriter.js";import{U64Mut as e}from"../primitive/number/U64.js";export class APrng64{constructor(t,e){this._state=t,this.saveable=e}save(){return this.saveable?this._state.toBytesLE():new Uint8Array(0)}_nextU(t){const r=e.fromI32s(0,0);let i=t;for(;i>this.bitGen;)i-=this.bitGen,r.orEq(this.rawNext().lShift(i));return i==this.bitGen?r.orEq(this.rawNext()):r.orEq(this.rawNext().rShift(this.bitGen-i))}nextBool(){return 1==this.rawNext().mut().rShiftEq(this.bitGen-1).low}nextByte(){return this._nextU(8).low}nextU16(){return this._nextU(16).low}nextI16(){let t=this._nextU(16).low>>>0;return t>32767&&(t=~(65535-t)),t}nextU31(){return this._nextU(31).low}nextU32(){return this._nextU(32).low}nextU64(){return this._nextU(64)}*seqU64(t=1){let e=0;for(;e++<t;)yield this._nextU(64)}nextI32(){return 0|this._nextU(32).low}nextF32(){return 5.960464477539063e-8*this._nextU(24).low}nextF64(){const t=this._nextU(53);return 11102230246251565e-32*(4294967296*t.high+t.low)}fillBytes(e){const r=t.mount(e),i=this.bitGen-this.safeBits,n=this.bitGen-32,s=32-i;for(;!r.full;){const t=this.rawNext();r.pushNumberBE(t.high,n),r.pushNumberBE(t.low>>>i,s)}return e}}