JavaScript 版本:
Math.floor((1+Math.random())*65536).toString(16).substring(1)
我能想到的Python版本:
def _get_number_in_hex():
tmp = int((1+random.random())*65536)
return hex(tmp)[3:]
我的Python版本有什么问题吗,应该和js版本一样。
最佳答案
不要费心去复制那个垃圾生成器。使用随机字节并将它们编码为十六进制(如果这是您想要的)
import os
os.urandom(4).encode('hex')[0:4]
os.urandom(4).encode('hex')[0:4]
os.urandom(4).encode('hex')[0:4]
os.urandom(4).encode('hex')[0:4]
输出
0b93
72f8
7ec0
1204
关于javascript - 我怎样才能将这个随机生成器从 JS 版本转换为 Python 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33450066/