我怎么能把这个随机生成器从JS转换为Python版本

时间:2015-10-31 09:32:52

标签: javascript python

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版本相同。

1 个答案:

答案 0 :(得分:2)

不要试图复制那个垃圾生成器。使用随机字节并将它们编码为十六进制(如果这是你想要的)

@font-face {
  font-family: 'Roboto';
  font-style: normal;
  font-weight: 400;
  src: url("../fonts/Roboto-Regular.woff2") format('woff2'), url("../fonts/Roboto-Regular.ttf") format('truetype');
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02C6, U+02DA, U+02DC, U+2000-206F, U+2074, U+20AC, U+2212, U+2215, U+E0FF, U+EFFD, U+F000;
}

输出

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]