2021年1月22日星期五

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:

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.  const build16 = (x, o) => fetch16((fetch16(x) + o) ^ 42703)  const build8 = (x, o) => fetch8((fetch8(x) + o) ^ 101)    let i = 0  let invalid = []  while (i < 255) {    let j = 0    while (j < 255) {      let x = build8(i, j)      if (x > 255) {        invalid.push([ i, j, x ])      }      j++    }    i++  }    console.log(JSON.stringify(invalid))

However, while the fetch8 and fetch16 functions properly cycle through the entire set of numbers before repeating, the build8 and build16 functions don't, they go outside of the desired range, see the output from the above code. For example, when i = 11 and j = 184, x = 340, which is > 255.

However, the output from these build8 and build16 functions is fantastic. It appears entirely random and doesn't repeat any values before going through the whole set.

How can I modify these build8 and build16 functions so that they only include numbers within the set (0-255, or 0-65535), yet appear entirely random like they do here, and yet never repeat a value before going through all of them?

I'm not entirely sure how the author of the post landed on fetch8((fetch8(x) + o) ^ 101) for example, doing that XOR and passing in values like this. But the end result appears very random. I just would like to make it so the output is:

  • Within the desired range of values.
  • Doesn't repeat any value until all values have been enumerated.
  • Appears entirely random like build8 does.
https://stackoverflow.com/questions/65855154/how-to-get-this-prng-to-generate-numbers-within-the-range January 23, 2021 at 10:31AM

没有评论:

发表评论