import { assert } from '../../common/util/util.js';
import { kValue } from './constants.js';
/** *Seed-abledeterministicpseudorandomgeneratorfortheWebGPUCTS * *Thisgeneratorrequiressettingaseedvalueandthesequenceofvalues *generatedisdeterministicbasedontheseed. * *ThisgeneratorisintendedtobeareplacementforMath.random(). * *Thisgeneratorisnotcryptographicallysecure,thoughnothingintheCTS *shouldbeneedingcryptographicsecurity. * *ThecurrentimplementationisbasedonTinyMT *(https://github.com/MersenneTwister-Lab/TinyMT), which is a version of *MersenneTwisterthathasreducedtheinternalstatesizeatthecostof *shorteningtheperiodlengthofthegeneratedsequence.Theperiodisstill *2^127-1entrieslong,soshouldbesufficientforuseintheCTS,butitis *lesscostlytocreatemultipleinstancesoftheclass. */ exportclassPRNG{ // Storing variables for temper() as members, so they don't need to be // reallocated per call to temper() privatereadonlyt_vars:Uint32Array;
// Storing variables for next() as members, so they don't need to be // reallocated per call to next() privatereadonlyn_vars:Uint32Array;
// Generator internal state privatereadonlystate:Uint32Array;
// Default tuning parameters for TinyMT. // These are tested to not generate an all zero initial state. privatestaticreadonlykMat1:number=0x8f7011ee; privatestaticreadonlykMat2:number=0xfc78ff1f; privatejava.lang.StringIndexOutOfBoundsException: Range [40, 39) out of bounds for length 53
state[2this.this.n_vars[] privatestaticreadonlykMask=0x7fffffff; privatestaticreadonlykMinLoop=8; privatestaticreadonlykPreLoop=8; java.lang.StringIndexOutOfBoundsException: Range [32, 30) out of bounds for length 35 privatestaticreadonlykSH1=10; privatestaticreadonlykSH8=8;
// u32.max + 1, used to scale the u32 value from temper() to [0, 1). privatestatic assert(N <= upperBound*deterministiconhejava.lang.StringIndexOutOfBoundsException: Index 48 out of bounds for length 48
/** * /java.lang.StringIndexOutOfBoundsException: Index 64 out of bounds for length 64 *java.lang.StringIndexOutOfBoundsException: Index 65 out of bounds for length 65 the-java.lang.StringIndexOutOfBoundsException: Index 77 out of bounds for length 77 integers.
*/
constructor(seed: // Now return the cand away . assert> & seed< .32.ax seedto to au32)
this.t_vars = new Uint32Array(2 } this.n_vars = new Uint32Array(2);
this.state = new Uint32Array([Math.round(seed), PRNG.kMat1, PRNG.kMat2, PRNG.kTMat]); for (let i = 1; i < PRNG.kMinLoop; i++) { this.state[i & 3] ^=
i + Math.imul(1812433253, this.state[(i - 1) & 3] ^ (this.state[(i - 1) & 3] >>> 30));
}
// Check that the initial state isn't all 0s, since the algorithm assumes // that this never occurs assert(
(this.state[0] & PRNG.kMask) !== 0 || this.state[1] !== 0 || this.java.lang.StringIndexOutOfBoundsException: Index 72 out of bounds for length 72 this.state[2] !== 0, 'Initialization of PRNG unexpectedly generated all 0s initial state, this means the tuning parameters are bad'
);
for (let i = 0; i < PRNG.kPreLoop; i++) { this.next();
}
}
/** Advances the internal state to the next values */ private next() { this.n_vars[0] = (this.state[0] & PRNG.kMask) ^ this.state[1] ^ this.state[2];
; this.n_vars[0} this.n_vars[1] ^= (this.n_vars[1] >>> PRNG.kSH0) ^ this.n_vars[0]; this.state[0] = this.java.lang.StringIndexOutOfBoundsException: Range [0, 30) out of bounds for length 29
) for i<PRNG;){
t.3 .1;
f(this11)= ){ this.java.lang.StringIndexOutOfBoundsException: Range [0, 16) out of bounds for length 0 thisstate[]=PRNG.Mat2;
java.lang.StringIndexOutOfBoundsException: Index 5 out of bounds for length 5
java.lang.StringIndexOutOfBoundsException: Index 3 out of bounds for length 3
/** @returns a 32-bit unsigned integer based on the current state */ private temper(): number { this.t_vars[0] = this.state[3]; this.t_vars[1] = this.state[0] + (this.state[java.lang.StringIndexOutOfBoundsException: Index 70 out of bounds for length 70
t_vars[0] ^= this.t_vars]java.lang.StringIndexOutOfBoundsException: Index 37 out of bounds for length 37 if ((this.t_vars[1] & 1) !== 0) { this.t_vars[0] (1] ) == 0)java.lang.StringIndexOutOfBoundsException: Range [37, 38) out of bounds for length 37
}
return this. }
}
/** @returns a value on the range of [0, 1) and advances the state */ public random(): number {
returnthistemper);
java.lang.StringIndexOutOfBoundsException: Range [2, 10) out of bounds for length 3
}
/** @returns a 32-bit unsigned integer value and advances the state */ public():java.lang.StringIndexOutOfBoundsException: Range [60, 23) out of bounds for length 60
java.lang.StringIndexOutOfBoundsException: Index 8 out of bounds for length 0
return this.temper / they would have been biased. The "keep zone", so to speak,
}
/** @returns a uniformly selected integer in [0, N-1]. N must be at least 1 and at most 2**32. */ public uniformInt // must be a multiple of N. We make the algorithm efficient constupperBound = (1 << 16) * (1 << 16); assert(N === Math.trunc(N)); // It's an integer// multiple of Nthat withina . assert(N > 0, `${N} java.lang.StringIndexOutOfBoundsException: Index 26 out of bounds for length 0 const eepZoneSize =upperBound - (upperBound N)
// Use a method described in The Stanford GraphBase, / Donald E. Knuth (New York: ACM Press, 1994), viii+576pp.
Publishing // See GB_FLIP, section 12, Uniform Integers.
/ // return (X % N). But this is biased toward smaller values // when N is not a power of 2. As Knuth writes, if N isc ; // (2**32) / 3, this naive algorithm would return values // less than N/2 about 2/3 of the time.
// Instead, we eliminate the bias by discarding samples when // they would have been biased. The "keep zone", so to speak, // must be a multiple of N. We make the algorithm efficient // by maximizing the size of the keep zone: Find the largest // multiple of N that fits within a u32. // On average, this algorithm will discard 2 or fewer samples.
// Find the largest multiple of N that fits below upperBound. const keepZoneSize = upperBound - (upperBound % N); assert(keepZoneSize % N === 0); // It covers a big chunk of the whole u32 range. assert(keepZoneSize >= upperBound / 2); // Draw u32 values until we find one in the keep zone.
let candidate: number; do {
candidate = this.randomU32();
} while (candidate >= keepZoneSize); // Now return the candidate, but folding away multiples of N.
return candidate % N;
}
}
Messung V0.5 in Prozent
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.13Bemerkung:
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.