基于這個問題如何讓這個 PRNG 生成范圍內的數字?,我讓它在 JavaScript 中支持 BigInt(大值),我認為我是對的(如果我做錯了,請糾正我):
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);
旁注:永遠不應該有一個invalid值。
但主要的問題是,給定x諸如的值40531205068036774067539981357810868028938588n,如何將其劃分為 5 位值的陣列(0-31 之間的整數陣列)?
是不是像做x.toString(32)然后將這些字母轉換為索引或其他東西一樣簡單?不確定我是否正確執行此操作。
uj5u.com熱心網友回復:
要創建一個數字在 0-31 之間的陣列,您只需將余數相除和收集(然后將其轉換為標準數字):
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);
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/403624.html
標籤:
