Math.random() 產生偽隨機數,種子可預測,不可用於驗證碼、Token、密碼、抽獎等安全場景。
// 瀏覽器
const arr = new Uint32Array(1);
crypto.getRandomValues(arr);
const secureRandom = arr[0] / 0x100000000; // [0, 1)
// Node.js
const crypto = require('crypto');
crypto.randomBytes(4).readUInt32BE(0) / 0x100000000;
Math.random() + Date.now() + Math.random()。