I have asked a similar question before: How to get this PRNG to generate numbers within the range?
How do I take that and make it work for a number which can be up to 5 bits (32 possible values)? I tried this but it is giving "invalid" values:
const fetch = (x, o) => {
if (x >= o) {
return x
} else {
const v = (x * x) % o
return (x <= o / 2) ? v : o - v
}
}
const fetch16 = (x) => fetch(x, 65519)
const fetch8 = (x) => fetch(x, 251)
// the last number can be anything.
// MODIFIED THIS
const build32 = (x, o) => fetch8((fetch8(x) + o) % 256 ^ 101) % 32
const j = 115; // If you don't want duplicates, either i or j should stay fixed
let i = 0
let invalid = [];
let valid = new Set;
while (i <= 32) { // <-- small fix here!
let x = build32(i, j); // To test, you can swap i and j here, and run again.
if (x > 31 || valid.has(x)) {
invalid.push([i, j, x]);
} else {
valid.add(x);
}
i++;
}
console.log("invalid:", invalid);
console.log("valid:", [...valid]);
console.log("count of valid:", valid.size);
The system should iterate through the numbers 0-31 without repeating, in what seems like random order.