Skip to content Skip to sidebar Skip to footer

How To Get This Prng To Generate Numbers Within The Range?

I found this which I made into this below, for 8 and 16 bit numbers in JavaScript: However, while the fetch8 and fetch16 functions properly cycle through the entire set of numbers

Solution 1:

The reason you get out of range values is that although fetchXX will produce a value in range, the + o spoils this property. The XOR operation may sometimes bring it back in range, but not always.

So you should take the modulo of the value after + o. The XOR operation will never bring it out of range, so that can stay as it is.

Secondly, to test whether no duplicates are generated, you would need to fix one of the two arguments passed to the buildXX function and only vary the other. It seems more logical to me to freeze the second argument.

So this is what it would look like:

constfetch = (x, o) => {
  if (x >= o) {
    return x
  } else {
    const v = (x * x) % o
    return (x <= o / 2) ? v : o - v
  }
}

constfetch16 = (x) => fetch(x, 65519)
constfetch8 = (x) => fetch(x, 251)

// the last number can be anything.constbuild16 = (x, o) => fetch16((fetch16(x) + o) % 65536 ^ 42703)
constbuild8 = (x, o) => fetch8((fetch8(x) + o) % 256 ^ 101)

const j = 115; // If you don't want duplicates, either i or j should stay fixedlet i = 0let invalid = [];
let valid = newSet;
while (i <= 255) { // <-- small fix here!let x = build8(i, j); // To test, you can swap i and j here, and run again.if (x > 255) {
        invalid.push([ i, j, x ]);
    } else {
        valid.add(x);
    }
    i++;
}

console.log("invalid:", JSON.stringify(invalid));
console.log("count of valid:", valid.size);

Post a Comment for "How To Get This Prng To Generate Numbers Within The Range?"