Working version of updated randomness
This commit is contained in:
parent
ed5bf3b3fc
commit
058d950121
1 changed files with 36 additions and 26 deletions
|
|
@ -188,36 +188,46 @@ export const mouseX = signal(() => _mouseX);
|
||||||
|
|
||||||
// random signals
|
// random signals
|
||||||
|
|
||||||
const xorwise = (x) => {
|
// Produce "Avalanche effect" where flipping a single bit of x
|
||||||
const a = (x << 13) ^ x;
|
// results in all output bits flipping with probability 0.5
|
||||||
const b = (a >> 17) ^ a;
|
// See e.g. https://github.com/aappleby/smhasher/blob/0ff96f7835817a27d0487325b6c16033e2992eb5/src/MurmurHash3.cpp#L68-L77
|
||||||
return (b << 5) ^ b;
|
function _murmurHashFinalizer(x) {
|
||||||
};
|
x |= 0;
|
||||||
|
x ^= x >>> 16;
|
||||||
|
x = Math.imul(x, 0x85ebca6b);
|
||||||
|
x ^= x >>> 13;
|
||||||
|
x = Math.imul(x, 0xc2b2ae35);
|
||||||
|
x ^= x >>> 16;
|
||||||
|
return x >>> 0; // unsigned
|
||||||
|
}
|
||||||
|
|
||||||
// stretch 300 cycles over the range of [0,2**29 == 536870912) then apply the xorshift algorithm
|
// Used to decorrelate nearby t and i prior to hashing
|
||||||
const _frac = (x) => x - Math.trunc(x);
|
function _decorrelate(t, i = 0) {
|
||||||
|
// const T = Math.floor(t * 4096); // set 2^12 resolution
|
||||||
|
const T = Math.floor(t * 536870912); // set 2^29 resolution
|
||||||
|
const lowBits = (T >>> 0) >>> 0;
|
||||||
|
const highBits = Math.floor(T / 4294967296) >>> 0; // 2^32
|
||||||
|
let key = lowBits ^ Math.imul(highBits ^ 0x85ebca6b, 0xc2b2ae35);
|
||||||
|
key ^= Math.imul(i ^ 0x7f4a7c15, 0x9e3779b9);
|
||||||
|
return key >>> 0;
|
||||||
|
// return (T ^ Math.imul(i ^ 0x7f4a7c15, 0x9e3779b9)) >>> 0;
|
||||||
|
}
|
||||||
|
|
||||||
const timeToIntSeed = (x) => xorwise(Math.trunc(_frac(x / 300) * 536870912));
|
function randAt(t, i = 0) {
|
||||||
|
return _murmurHashFinalizer(_decorrelate(t, i)) / 4294967296; // 2^32
|
||||||
|
}
|
||||||
|
|
||||||
const intSeedToRand = (x) => (x % 536870912) / 536870912;
|
// N samples at time t
|
||||||
|
function timeToRands(t, n) {
|
||||||
|
const out = new Array(n);
|
||||||
|
for (let i = 0; i < n; i++) out[i] = randAt(t, i);
|
||||||
|
return out;
|
||||||
|
}
|
||||||
|
|
||||||
const timeToRand = (x) => Math.abs(intSeedToRand(timeToIntSeed(x)));
|
// Single sample at time t
|
||||||
|
function timeToRand(t) {
|
||||||
const timeToRandsPrime = (seed, n) => {
|
return randAt(t, 0);
|
||||||
const result = [];
|
}
|
||||||
// eslint-disable-next-line
|
|
||||||
for (let i = 0; i < n; ++i) {
|
|
||||||
result.push(intSeedToRand(seed));
|
|
||||||
seed = xorwise(seed);
|
|
||||||
}
|
|
||||||
return result;
|
|
||||||
};
|
|
||||||
|
|
||||||
const timeToRands = (t, n) => timeToRandsPrime(timeToIntSeed(t), n);
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A discrete pattern of numbers from 0 to n-1
|
* A discrete pattern of numbers from 0 to n-1
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue