从节点加密生成的随机值中消除破折号?

Eliminating dashes from node crypto generated random values?

NPM cuid 库中有这个函数:

import * as crypto from "crypto"

var lim = Math.pow(2, 32) - 1;

export function getRandomValue () {
  return Math.abs(crypto.randomBytes(4)
    .readInt32BE(0) / lim)
}

来自此的 return 值不应 return 带有破折号的值。

However per my test sampling a million values, one value returned contains a dash.

我们如何消除破折号?

有人在较早的问题中建议使用 % 而不是 /,这很有效。我 运行 1000 万个样本,其中 none 个包含破折号,所以这对你们其他人来说似乎是正确的做法吗?

"dash" 不是连字符。

这是数字的科学记数法,例如8.55652615627193e-7

有关类似问题,请参阅 'e' in javascript numbers

您可以使用 num.toString(16) 转换为十六进制,看起来像 0.00000e5b00000e5b