首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >纯随机数发生器

纯随机数发生器
EN

Code Review用户
提问于 2022-03-19 15:09:25
回答 2查看 117关注 0票数 0

我认为地球上最好的随机数是PI。我编写了一个用Python生成伪随机数列表的代码。然后,它将这个列表作为Pi中数字的位置,然后在这些位置输出数字。我期待有人告诉我这个方法不够随机。

代码语言:javascript
复制
import random

# Read all decimal places of PI from the file
# and store as a string of 1 Million digits

with open("piDigits.txt", 'r') as f:
    pidecimals = f.read()
f.close()

# Generate a list of random numbers
# Treat each random number as a position in the expansion of Pi
# Go to that position and pick up a particular number of digits of Pi
# Output the results as a list

def generate_random_array(nums, lengthofnum):
    randomnumbers=[]
    random_array = random.sample(range(1,len(pidecimals)-lengthofnum), nums)
    for i in range(0, nums):
        randomnumbers.append(pidecimals[random_array[i]:random_array[i]+lengthofnum])
    print(randomnumbers)

# Main Program

mychoice = int(input("How many random numbers do you want? "))
mylength = int(input("How many digits in each random number? "))

generate_random_array(mychoice, mylength)

样本输出:

代码语言:javascript
复制
How many random numbers do you want? 10000
How many digits in each random number? 2

['55', '37', '93', '63', '36', '35', '72', '81', '01', '46', '90', '98', '31', '38', '65', '57', '10', '42', '07', '72', '11', '68', '48', '33', '08', '43', '28', '64', '75', '48', '00', '37', '74', '05', '97', '72', ,...]

经过以下评论中的讨论,我做了一个进一步的实验。我用量子随机库生成了一系列的量子随机数。然后我提取它的十进制部分,然后从它中提取一些数字。然后我测试了PI中存在的相同的字符串。瞧!他们做到了!

代码语言:javascript
复制
import quantumrandom as qr

with open("piDigits.txt", 'r') as f:
    pidecimals = f.read()
f.close()

maxrange = int(input("Enter Maxmimum of Range "))
count = 0

for i in range (0,maxrange):
    myx = qr.randint()
    decimalstring = str(myx)
    substring = decimalstring[2:7]
    if substring in pidecimals:
        count += 1
    print(f"Quantum digit sequence {substring} found in Pi")
print(f"Quantum digit sequences found in Pi {count} times out of {maxrange}")

5位数和10个量子随机数..。

代码语言:javascript
复制
Enter Maxmimum of Range 10
Quantum digit sequence 39467 found in Pi
Quantum digit sequence 58304 found in Pi
Quantum digit sequence 11932 found in Pi
Quantum digit sequence 93949 found in Pi
Quantum digit sequence 68543 found in Pi
Quantum digit sequence 84222 found in Pi
Quantum digit sequence 10429 found in Pi
Quantum digit sequence 12771 found in Pi
Quantum digit sequence 70878 found in Pi
Quantum digit sequence 08628 found in Pi
Quantum digit sequences found in Pi 10 times out of 10

对于6个数字和10个量子随机数:

代码语言:javascript
复制
Enter Maxmimum of Range 10
Quantum digit sequence 122453 found in Pi
Quantum digit sequence 817349 found in Pi
Quantum digit sequence 719539 found in Pi
Quantum digit sequence 609292 found in Pi
Quantum digit sequence 824750 found in Pi
Quantum digit sequence 833676 found in Pi
Quantum digit sequences found in Pi 6 times out of 10

如果我从末端截断随机序列,程序也会找到序列,因此它将量子数的后半部分与PI中的序列相匹配。

也许这就是PI与量子随机数并列的全部。:)

EN

回答 2

Code Review用户

回答已采纳

发布于 2022-03-19 16:08:35

这一解决方案的基本前提是不正确的。现有的PRNG (random.sample)是这样定义的:

几乎所有的模块函数都依赖于基本函数random(),它在半开放范围内均匀地产生随机浮动[0.0,1.0]。Python使用Mersenne作为核心生成器。它产生53位精度浮标,周期为2**19937-1.C中的底层实现是快速的和线程安全的。Mersenne是目前测试最广泛的随机数发生器之一。

使用它作为在固定数字的替换表中查找的索引,这些数字的分布是一致的--不管它们是什么,pi或其他--都不会增加熵。所以说它比random.sample()更多(或更少)是随机的,这是不正确的;它与random.sample()完全一样随机。只是速度更慢,占用了更多的内存和磁盘空间。不要这样做。

如果你想要“更随意”的东西:为什么?如果它是轻浮的,只需使用内置随机模块。如果是出于安全目的,请使用秘密

票数 4
EN

Code Review用户

发布于 2022-03-20 04:16:40

生成方法是有缺陷的,很容易看出原因。只需要一个有一百万位数的随机数。再次运行程序,并要求一个新的100万数字随机数。不是很随机,如果它总是给你同样的价值。如果你问超过一个随机数,程序崩溃!

你只能得到两个不同的999,999位随机数,和三个999,998位数,以此类推。这个限制结果滥用了只提供一百万位数的限制,但它表明了算法中的一个缺陷--它总是有一个限制,限制了随机数生成π的熵…。这个限制远远小于内置在Python中的Mersenne算法。

票数 1
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/275083

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档