久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何讓這個 PRNG 生成范圍內的數字?

How to get this PRNG to generate numbers within the range?(如何讓這個 PRNG 生成范圍內的數字?)
本文介紹了如何讓這個 PRNG 生成范圍內的數字?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我發現 這個 我在下面做了這個,用于 JavaScript 中的 8 位和 16 位數字:

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))

然而,雖然 fetch8fetch16 函數在重復之前循環遍歷整個數字集,但 build8build16 函數沒有,它們超出了所需的范圍,請參閱上述代碼的輸出.例如,當i = 11j = 184時,x = 340,即>;255.

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.

但是,這些 build8build16 函數的輸出非常棒.它看起來完全是隨機的,并且在遍歷整個集合之前不會重復任何值.

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.

如何修改這些 build8build16 函數,使它們僅包含集合中的數字(0-255 或 0-65535),但看起來完全隨機就像他們在這里做的那樣,但在遍歷所有值之前從不重復一個值?

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?

我不完全確定帖子的作者是如何登陸 fetch8((fetch8(x) + o) ^ 101) 的,例如,執行 XOR 并傳入這樣的值.但最終結果顯得非常隨機.我只是想這樣做,所以輸出是:

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:

  • 在所需的值范圍內.
  • 在枚舉完所有值之前不重復任何值.
  • 看起來像 build8 一樣完全隨機.

推薦答案

你得到超出范圍值的原因是雖然 fetchXX 會產生一個范圍內的值,但 + o 破壞了這個屬性.XOR 操作可能有時會將其帶回范圍內,但并非總是如此.

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.

所以你應該取 + o 之后的值的模.XOR 操作永遠不會使其超出范圍,因此可以保持原樣.

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.

其次,要測試是否沒有生成重復項,您需要修復傳遞給 buildXX 函數的兩個參數之一,只改變另一個.凍結第二個論點對我來說似乎更合乎邏輯.

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.

這就是它的樣子:

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) % 65536 ^ 42703)
const build8 = (x, o) => fetch8((fetch8(x) + o) % 256 ^ 101)

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 <= 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);

這篇關于如何讓這個 PRNG 生成范圍內的數字?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Using discord.js to detect image and respond(使用 discord.js 檢測圖像并響應)
Check if user ID exists in Discord server(檢查 Discord 服務器中是否存在用戶 ID)
Guild Member Add does not work (discordjs)(公會成員添加不起作用(discordjs))
Creating my first bot using REPLIT but always error Discord.JS(使用 REPLIT 創建我的第一個機器人,但總是錯誤 Discord.JS)
How do I code event/command handlers for my Discord.js bot?(如何為我的 Discord.js 機器人編寫事件/命令處理程序?)
How to find a User ID from a Username in Discord.js?(如何從 Discord.js 中的用戶名中查找用戶 ID?)
主站蜘蛛池模板: 91国内精精品久久久久久婷婷 | 亚洲高清视频在线 | 日韩在线播放第一页 | 永久免费视频 | 日韩一级免费电影 | 凹凸日日摸日日碰夜夜 | 日韩在线精品视频 | 久久尤物免费一区二区三区 | 欧美激情啪啪 | 亚洲成人精品久久久 | 日日干夜夜操 | 精品自拍视频 | 伊人性伊人情综合网 | 欧美性生活免费 | 亚洲一区二区免费视频 | 国产精品美女久久久久aⅴ国产馆 | 99久久婷婷国产综合精品电影 | 免费av直接看 | 国产亚洲精品精品国产亚洲综合 | 欧美精品成人一区二区三区四区 | 在线成人免费视频 | 中文字幕在线观看第一页 | 久久国产精品视频 | 亚洲综合无码一区二区 | 91精品国产91久久久久久密臀 | 精品欧美视频 | 成人网在线 | 久久久久久久av | 国内自拍偷拍 | 欧美一区二区三区在线观看 | 天堂三级 | 亚洲欧美一区二区三区国产精品 | 丁香六月激情 | 91超碰在线 | 久久久久黄 | 亚洲成人福利在线观看 | 最新av在线播放 | 在线日韩中文字幕 | 伊人一区 | 午夜一级黄色片 | 亚洲免费一区 |