Files
wechat-minigame/js/gameplay/difficulty/random.js
2026-03-29 00:36:28 +08:00

37 lines
924 B
JavaScript

export function hashSeed(input) {
const value = String(input)
let hash = 2166136261
for (let index = 0; index < value.length; index += 1) {
hash ^= value.charCodeAt(index)
hash = Math.imul(hash, 16777619)
}
return hash >>> 0
}
export function createRng(seed) {
let state = typeof seed === 'number' ? seed >>> 0 : hashSeed(seed)
return function next() {
state += 0x6D2B79F5
let value = state
value = Math.imul(value ^ (value >>> 15), value | 1)
value ^= value + Math.imul(value ^ (value >>> 7), value | 61)
return ((value ^ (value >>> 14)) >>> 0) / 4294967296
}
}
export function shuffleWithRng(items, rng) {
const result = [...items]
for (let index = result.length - 1; index > 0; index -= 1) {
const swapIndex = Math.floor(rng() * (index + 1))
const temp = result[index]
result[index] = result[swapIndex]
result[swapIndex] = temp
}
return result
}