js 生成随机数的方式

js 生成随机数的方式

  • Math.random()
  • new Date().getTime()
  • randomjs
  • globalThis.crypto.getRandomValues
globalThis.crypto.getRandomValues(new Uint32Array(10))
// logs
Uint32Array(10) [4070084714, 3331714512, 167030621, 3861175006, 162926641, 1318663833, 2059866687, 3205865186, 753962239, 1649391101, buffer: ArrayBuffer(40), byteLength: 40, byteOffset: 0, length: 10, Symbol(Symbol.toStringTag): 'Uint32Array']
0:4070084714
1:3331714512
2: 167030621
3: 3861175006
4: 162926641
5: 1318663833
6: 2059866687
7: 3205865186
8: 753962239
9: 1649391101
buffer: ArrayBuffer(40)
byteLength: 40
byteOffset: 0
length: 10
Symbol(Symbol.toStringTag): "Uint32Array"
[[Prototype]]: TypedArray