作业帮 > 英语 > 作业

关于Python中的随机数生成步骤和随机数质量

来源:学生作业帮 编辑:百度作业网作业帮 分类:英语作业 时间:2024/05/03 22:21:18
关于Python中的随机数生成步骤和随机数质量
求大神讲解写Python2.7里面random随机数生成模块中随机数生成的详细步骤,并求评价该随机数质量如何.答辩要用,急
关于Python中的随机数生成步骤和随机数质量
>>> random.random()        # Random float x, 0.0 <= x < 1.0
0.37444887175646646
>>> random.uniform(1, 10)  # Random float x, 1.0 <= x < 10.0
1.1800146073117523
>>> random.randint(1, 10)  # Integer from 1 to 10, endpoints included
7
>>> random.randrange(0, 101, 2)  # Even integer from 0 to 100
26
>>> random.choice('abcdefghij')  # Choose a random element
'c'

>>> items = [1, 2, 3, 4, 5, 6, 7]
>>> random.shuffle(items)
>>> items
[7, 3, 2, 5, 6, 4, 1]

>>> random.sample([1, 2, 3, 4, 5],  3)  # Choose 3 elements
[4, 1, 5]
测试随机数质量一般是用以下几种测试:

Birthday spacings
Overlapping permutations
Ranks of matrices
Monkey tests
Count the 1s
Parking lot test
Minimum distance test
Random spheres test
The squeeze test
Overlapping sums test
Runs test
The craps test

再问: 谢谢。其实我想知道,系统是如何从读取系统时间到生成随机数的过程,而不是怎么用random函数。唉
再答: 具体的方法在Lib/random.py和Modules/_randommodule.c里面。另外,python的seed不一定是系统时间,在linux会优先 /dev/urandom