问题描述
NPM cuid
库中有此功能:
There's this function in the NPM cuid
library:
import * as crypto from "crypto"
var lim = Math.pow(2, 32) - 1;
export function getRandomValue () {
return Math.abs(crypto.randomBytes(4)
.readInt32BE(0) / lim)
}
此值的返回值不应返回带短划线的值。
The return value from this should not return values with dashes in it.
。
如何消除破折号?
前面一个问题中的某个人建议使用%
代替 /
,这可行。我运行了1000万个样本,但其中没有一个包含破折号,所以这对其他人来说似乎是对的吗?
Someone in an earlier question suggested using %
instead of /
and this works. I ran 10 million samples and none of them contain dashes, so does this seem like the correct thing to do to the rest of you?
推荐答案
连字符不是连字符。
它是科学数字的表示法,例如 8.55652615627193e-7
。
It's the scientific notation for a number such as 8.55652615627193e-7
.
请参见
您可以使用 num.toString(16)
转换为类似于 0.00000e5b00000e5b
。
You could use num.toString(16)
to convert to hexadecimal which looks like 0.00000e5b00000e5b
.
这篇关于消除节点加密生成的随机值中的破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!