下面是Python 3代码,它在0.28秒内生成1000000个“随机”小写字母(另请参见最后的0.11-秒解决方案;@Ashwini Chaudhary的问题代码在我的机器上需要0.55秒,@Markku K.的代码——0.53):#!/usr/bin/env python3
import os
import sys
def write_random_lowercase(n):
min_lc = ord(b'a')
len_lc = 26
ba = bytearray(os.urandom(n))
for i, b in enumerate(ba):
ba[i] = min_lc + b % len_lc # convert 0..255 to 97..122
sys.stdout.buffer.write(ba)
write_random_lowercase(1000000)
% len_lc虽然它仍然满足条件(ascii,小写,1,2,3个字母序列的频率),但它扭曲了分布(请参见末尾的如何修复):$ python3 generate-random.py | python3 check-seq.py
其中check-seq.py:#!/usr/bin/env python3
import sys
from collections import Counter
from string import ascii_lowercase
def main():
limits = [40000, 2000, 100]
s = sys.stdin.buffer.readline() # a single line
assert 1000000 <= len(s) <= 1000002 # check length +/- newline
s.decode('ascii','strict') # check ascii
assert set(s) == set(ascii_lowercase.encode('ascii')) # check lowercase
for n, lim in enumerate(limits, start=1):
freq = Counter(tuple(s[i:i+n]) for i in range(len(s)))
assert max(freq.values()) <= lim, freq
main()
注:在acm.timus.rugenerate-random.py上给出“超出输出限制”。
要提高性能,可以使用^{} method(0.11秒):#!/usr/bin/env python3
import os
import sys
# make translation table from 0..255 to 97..122
tbl = bytes.maketrans(bytearray(range(256)),
bytearray([ord(b'a') + b % 26 for b in range(256)]))
# generate random bytes and translate them to lowercase ascii
sys.stdout.buffer.write(os.urandom(1000000).translate(tbl))
如何修复% len_lc倾斜
256(字节数)不能被26(低位拉丁字母数)整除,因此公式min_lc + b % len_lc使某些值出现的频率低于其他值,例如:#!/usr/bin/env python3
"""Find out skew: x = 97 + y % 26 where y is uniform from [0, 256) range."""
from collections import Counter, defaultdict
def find_skew(random_bytes):
char2freq = Counter(chr(ord(b'a') + b % 26) for b in random_bytes)
freq2char = defaultdict(set)
for char, freq in char2freq.items():
freq2char[freq].add(char)
return {f: ''.join(sorted(c)) for f, c in freq2char.items()}
print(find_skew(range(256)))
# -> {9: 'wxyz', 10: 'abcdefghijklmnopqrstuv'}
这里,输入range(256)是均匀分布的(每个字节只出现一次),但是输出中的'wxyz'字母的出现频率低于其余的9与10的出现频率。要修复此问题,可以删除未对齐的字节:print(find_skew(range(256 - (256 % 26))))
# -> {9: 'abcdefghijklmnopqrstuvwxyz'}
这里,输入是在[0, 234)范围内均匀分布的字节,输出是均匀分布的ascii小写字母。
bytes.translate()接受第二个参数以指定要删除的字节:#!/usr/bin/env python3
import os
import sys
nbytes = 256
nletters = 26
naligned = nbytes - (nbytes % nletters)
tbl = bytes.maketrans(bytearray(range(naligned)),
bytearray([ord(b'a') + b % nletters
for b in range(naligned)]))
bytes2delete = bytearray(range(naligned, nbytes))
R = lambda n: os.urandom(n).translate(tbl, bytes2delete)
def write_random_ascii_lowercase_letters(write, n):
"""*write* *n* random ascii lowercase letters."""
while n > 0:
# R(n) expected to drop `(nbytes - nletters) / nbytes` bytes
# to compensate, increase the initial size
n -= write(memoryview(R(n * nbytes // naligned + 1))[:n])
write = sys.stdout.buffer.write
write_random_ascii_lowercase_letters(write, 1000000)
如果随机生成器(os.urandom)生成超出对齐范围(>=234)的长字节序列,则while循环可能会执行多次。
如果使用^{}而不是^{},时间性能可以提高一个数量级。前者使用Mersenne Twister作为核心生成器,其速度可能比使用操作系统提供的源代码的os.urandom()快。如果使用随机字符串作为机密,则后者更安全。