# Python 超快生成大量随机数的方法

75 篇文章 9 订阅
125 篇文章 5 订阅

### 1.random.randint

import random
UINT32upBound=0xffffffff
for i in range(0,10**4):
random.randint(0,UINT32upBound)


### 2./dev/urandom->os.urandom

import os
for i in range(0,10**4):
int(os.urandom(4).hex(),16)


dd if=<(openssl enc -aes-256-ctr -pass pass:"\$(dd if=/dev/urandom bs=128 count=1 2>/dev/null | base64)" -nosalt < /dev/zero) of=filename bs=1M count=100 iflag=fullblock


### 4.numpy

import numpy as np
l= np.random.randint(low=-2147483648, high=0x7FFFFFFF, size=10**4)
for i in range(0,10**4):
l[i]+2147483648


### 5. AES CTR

import os
from Crypto.Cipher import AES
from Crypto.Util import Counter
def AES_32_YIELD():
key=os.urandom(32)
aes = AES.new(key, AES.MODE_CTR, counter=Counter.new(128))
data = "0" * 16
data=data.encode('utf-8')
while True:
yield int(aes.encrypt(data).hex(),16)
yield_32=AES_32_YIELD()
for i in range(0,10**4):
next(yield_32)


## 综合测试和总结

Python中常用的测试方法是使用timeit这个库进行速度的测量，timeit是一个非常好用的库，用来测试代码运行速度的。

random.random()                         0.02370711136609316
np.random.rand()                        0.07226962689310312
********************
int(random.random()*0xffff)             0.051626757718622684
random.randint(0, 0xffff)               0.24031009431928396
int(os.urandom(4).hex(),16)             0.15973901469260454
hashlib.md5("md5 test".encode("utf-8")) 0.07776997052133083
np.random.randint(0xffff)               0.21123502869158983
Crypto.Random.random.getrandbits(32)    6.919489985331893
random.getrandbits(32)                  0.03729246836155653
********************
l.append(random.randint(0, 0xffff))     0.21211601048707962
np.random.randint(0xffff,size=count)    0.0007560765370726585
random.shuffle(a)                       0.14006372820585966


# -*- coding: utf-8 -*-
"""
Created on Wed Jul 17 19:57:55 2019

@author: sidanzhang
"""

import timeit
import random
import time
import datetime
import json
import hashlib
import os
from Crypto.Cipher import AES
from Crypto.Util import Counter
import Crypto.Random.random

import numpy as np

count = 10 ** 5

def func1():
random.random()

def func2():
int(random.random() * 0xffff)

def func3():
random.randint(0, 0xffff)

def func4():
int(os.urandom(4).hex(), 16)

def func5():
hashlib.md5("md5 test".encode("utf-8"))

def func6():
np.random.rand()

def func7():
np.random.random_integers(0, 0xffff)

def func8():
np.random.randint(0xffff)

def func9():
global b, count
b = np.random.randint(0xffff, size=count)

def func10():
a = [i for i in range(0, count)]
random.shuffle(a)

def func11():
l = []
for i in range(0, count):
l.append(random.randint(0, 0xffff))

def func12():
Crypto.Random.random.getrandbits(32)

def func13():
random.getrandbits(32)

print('random.random()                        ', timeit.timeit(func1, number=count))
print('np.random.rand()                       ', timeit.timeit(func6, number=count))
print("*" * 20)
print('int(random.random()*0xffff)            ', timeit.timeit(func2, number=count))
print('random.randint(0, 0xffff)              ', timeit.timeit(func3, number=count))
print('int(os.urandom(4).hex(),16)            ', timeit.timeit(func4, number=count))
print('hashlib.md5("md5 test".encode("utf-8"))', timeit.timeit(func5, number=count))
print('np.random.randint(0xffff)              ', timeit.timeit(func8, number=count))
print('Crypto.Random.random.getrandbits(32)   ', timeit.timeit(func12, number=count))
print('random.getrandbits(32)                 ', timeit.timeit(func13, number=count))
print("*" * 20)
print('l.append(random.randint(0, 0xffff))    ', timeit.timeit(func11, number=1))
print('np.random.randint(0xffff,size=count)   ', timeit.timeit(func9, number=1))
print('random.shuffle(a)                      ', timeit.timeit(func10, number=1))



pypy的速度是比Python快的。

• 8
点赞
• 10
收藏
觉得还不错? 一键收藏
• 打赏
• 0
评论
04-11 113
01-15 3万+
05-07 9490
08-20 1万+

### “相关推荐”对你有帮助么？

• 非常没帮助
• 没帮助
• 一般
• 有帮助
• 非常有帮助

zhangpeterx

¥2 ¥4 ¥6 ¥10 ¥20

1.余额是钱包充值的虚拟货币，按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载，可以购买VIP、C币套餐、付费专栏及课程。