生成可种子数据的随机字符串 [英] Generating random string of seedable data

查看:37
本文介绍了生成可种子数据的随机字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种在 Python 中以类似于 os.urandom() 方法的方式生成 n 个字节的随机字符串的方法,除了提供一种方法为数据生成播种.

I'm looking for a way to generate a random string of n bytes in Python in a similar way to os.urandom() method except providing a way to seed the data generation.

到目前为止我有:

def genRandData(size):
    buf = chr(random.randint(0,255))
    for i in range(size-1):
        buf = buf + chr(random.randint(0,255))
    return str(buf)

但是这个功能很慢,在我的机器上生成一兆字节的数据大约需要 1.8 秒.有什么办法可以改善这个问题(或者是一种为 os.urandom 提供种子的方法).

However this function is very slow, generating a megabyte of data takes about 1.8 seconds on my machine. Is there any way of improving this (or alternatively a way to seed os.urandom).

推荐答案

NEW ANSWER

重新阅读 OP 的问题后,我现在明白这是关于原始字节,而不是 ascii 字符字符串

After re-reading OP's question, I understand now that it's about raw bytes, not ascii chars string

那么,这个怎么样?

import random
gl = 0
def randBytes(size):
    global gl
    nr = bytearray(random.getrandbits(8) for _ in xrange(size))
    gl = nr
    return

%timeit randBytes(1000000)
1 loops, best of 3: 262 ms per loop

In [27]: gl.__sizeof__()
Out[27]: 1087223

旧答案在这里

import random
import string
def generateRandomString(size):
    return(''.join(random.choice(string.ascii_letters) for i in range(size)))

注意事项:

一个 ascii 字符是 1 个字节.所以size"表示字符串的长度和字节大小.

One ascii character is 1 byte. So "size" denotes both length of string and size in bytes.

您可以使用 string.ascii_uppercase 或 ascii_lowercase 来区分大小写

You can use string.ascii_uppercase or ascii_lowercase to have either lower and uppercase

random.seed 可用于指定种子.

random.seed can be used to specify the seed.

random.seed([x])¶

random.seed([x])¶

初始化基本随机数生成器.可选参数 x 可以是任何可散列的对象.如果 x 被省略或 None,当前系统时间用来;当前系统时间也用于初始化生成器当模块第一次导入时.如果提供随机源操作系统使用它们代替系统时间(请参阅有关可用性的详细信息,请参阅 os.urandom() 函数.

Initialize the basic random number generator. Optional argument x can be any hashable object. If x is omitted or None, current system time is used; current system time is also used to initialize the generator when the module is first imported. If randomness sources are provided by the operating system, they are used instead of the system time (see the os.urandom() function for details on availability).

所以你可以:

    import random
    import string
    def generateRandomString(size, seed=None):
        if seed != None:
             random.seed(seed)
        return(''.join(random.choice(string.ascii_letters) for i in range(size)))

时间:

In [30]: %time generateRandomString(1000000)
Wall time: 554 ms
<and then output>

这篇关于生成可种子数据的随机字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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