消除节点加密生成的随机值中的破折号? [英] Eliminating dashes from node crypto generated random values?

查看:99
本文介绍了消除节点加密生成的随机值中的破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

请参见 JavaScript代码中的 e

您可以使用 num.toString(16)转换为类似于 0.00000e5b00000e5b

You could use num.toString(16) to convert to hexadecimal which looks like 0.00000e5b00000e5b.

这篇关于消除节点加密生成的随机值中的破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆