import { toHex } from "./utils" function cryptoRandomUint32(length: number): Uint32Array { const crypto = (typeof self !== "undefined" ? self.crypto : undefined) || (typeof globalThis !== "undefined" ? globalThis.crypto : undefined) if (!crypto) return undefined const vals = new Uint32Array(length) crypto.getRandomValues(vals) return vals } export function anyRandomUint32(length: number): Uint32Array { let r = cryptoRandomUint32(length) if (!r) { r = new Uint32Array(length) for (let i = 0; i < r.length; ++i) r[i] = (Math.random() * 0x1_0000_0000) >>> 0 } return r } export function randomUInt(max: number) { const arr = anyRandomUint32(1) return arr[0] % max } export function randomBytes(n: number) { const buf = anyRandomUint32(n) const r = new Uint8Array(buf.length) for (let i = 0; i < n; ++i) r[i] = buf[i] & 0xff return r } export function randomDeviceId() { const devId = anyRandomUint32(8) for (let i = 0; i < 8; ++i) devId[i] &= 0xff return toHex(devId) }