Skip to content

Instantly share code, notes, and snippets.

@etienne-dldc
Created October 19, 2020 08:48
Show Gist options
  • Save etienne-dldc/d8ab197f4138d565b626a813866373f2 to your computer and use it in GitHub Desktop.
Save etienne-dldc/d8ab197f4138d565b626a813866373f2 to your computer and use it in GitHub Desktop.
function randomSequenceOfUnique(intermediateOffset: number, seedBase = 1) {
// from https://preshing.com/20121224/how-to-generate-a-sequence-of-unique-random-integers/
let index = permuteQPR(permuteQPR(seedBase) + 0x682f01);
function permuteQPR(x: number) {
const prime = 16777199;
const halfPrime = 8388599;
if (x >= prime) return x; // The 17 integers out of range are mapped to themselves.
// squaring can cause exceeding 2^53
const residue = (x * x) % prime;
return x <= halfPrime ? residue : prime - residue;
}
function getNth(n: number) {
// >>> ensures conversion to unsigned int
return permuteQPR(((permuteQPR(n) + intermediateOffset) ^ 0x5bf036) >>> 0);
}
return () => {
const res = getNth(index);
index++;
return res;
};
}
export function randomSequenceOfUniqueColor() {
const gen = randomSequenceOfUnique(Math.floor(Math.random() * 10000));
return () => {
let num = gen().toString(16);
while (num.length < 6) {
num = '0' + num;
}
return '#' + num;
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment