Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

171
Views
Why is my algorithm repeating values instead of producing 32 unique values?

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.

about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!