-1

Building off of this question How to get this PRNG to generate numbers within the range? , I am making it support BigInt (big values) in JavaScript, and I think I have it right (please correct me if I did it incorrectly):

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

const fetchLarge = (x) => fetch(x, 43214321432143214321432143214321432143214321n)

// the last number can be anything.
const buildLarge = (x, o) => fetchLarge((fetchLarge(x) + o) % BigInt(Math.pow(32, 31)) ^ 101010101010104321432143214321n)

const j = 432143213214321432143214321432143214321n; // If you don't want duplicates, either i or j should stay fixed
let i = 1n
let invalid = [];
let valid = new Set;
while (i) {
  let x = buildLarge(i, j);
  if (valid.has(x)) {
    invalid.push([i, j, x]);
  } else {
    valid.add(x);
  }
  console.log(x)
  i++;
}

console.log("invalid:", invalid);
console.log("valid:", [...valid]);
console.log("count of valid:", valid.size);

Sidenote: There should never be an invalid value.

But the main question is, given a value of x such as 40531205068036774067539981357810868028938588n, how can you divide it into an array of 5-bit values (array of integers between 0-31)?

Is it just as simple as doing x.toString(32) and then converting those letters to indices or something? Not sure if I'm doing this correctly.

Lance
  • 75,200
  • 93
  • 289
  • 503
  • 1
    Do I understand correctly that your final question is not really dependent on the code you presented? – trincot Jan 04 '22 at 21:52
  • 1
    Simply repeatedly divide-with-remainder by 32 repeatedly? Or do `toString(2)` and split into groups of 5… – Bergi Jan 04 '22 at 21:54
  • "*Is it just as simple as doing `x.toString(32)` and then converting each letter back to a number?*" - yes, that also works – Bergi Jan 04 '22 at 21:55
  • Well partly it's a double check that I can convert the pseudorandom bigint properly to a string with custom characters. – Lance Jan 04 '22 at 21:55
  • Wait, you want a string with custom characters? Or an array of numbers between 0-31? – trincot Jan 04 '22 at 21:56
  • 1
    If you plan on running this in a browser, I suggest using a name other than `fetch`… – Heretic Monkey Jan 04 '22 at 21:57
  • I want the array of numbers between 0 and 31, then I will convert those to custom characters. – Lance Jan 04 '22 at 21:57

1 Answers1

2

To create an array with numbers between 0-31 you can just divide and collect the remainders (which you would then convert to standard number):

function createArray(n, mod=32n) {
    if (!n) return [0];
    let arr = [];
    while (n) {
        arr.push(Number(n % mod));
        n /= mod;
    }
    return arr;
}

let result = createArray(40531205068036774067539981357810868028938588n);

console.log(result);
trincot
  • 317,000
  • 35
  • 244
  • 286