Python 3操作系统 [英] Python 3 os.urandom

查看:85
本文介绍了Python 3操作系统的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在哪里可以找到有关os.urandom的完整教程或文档?我需要获得一个随机整数来从80个字符的字符串中选择一个字符.

Where can I find a complete tutorial or documentation on os.urandom? I need to get get a random int to choose a char from a string of 80 characters.

推荐答案

如果只需要一个随机整数,则可以使用

If you just need a random integer, you can use random.randint(a, b) from the random module.

如果出于加密目的需要它,请使用random.SystemRandom().randint(a, b),它使用os.urandom().

If you need it for crypto purposes, use random.SystemRandom().randint(a, b), which makes use of os.urandom().

import random

r = random.SystemRandom()
s = "some string"
print(r.choice(s)) # print random character from the string
print(s[r.randrange(len(s))]) # same

这篇关于Python 3操作系统的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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