简体   繁体   English

我的代码会产生伪随机数还是随机数?

[英]Does my code produce a pseudorandom or random number?

import time

point1 = time.time()

point2 = time.time()

point3 = time.time()

random_number = str((point1 + point2 - point3) * point1)

print(random_number[7])
print(random_number[8])
print(random_number[9])
print(random_number[10])

It was my homework to design an algorithm that prodeces pseudorandom numbers.设计一种产生伪随机数的算法是我的功课。 Would the above code be concidered to be pseudorandom?上面的代码会被认为是伪随机的吗? I am assuming it could be called that because the number outcome is unpredictable but i just wanted to make sure.我假设可以这样称呼它,因为数字结果是不可预测的,但我只是想确定一下。 Also, how do i print all the numbers on the same line?另外,如何在同一行上打印所有数字? When i do print(random_number[10][7][8][9]) , i get an error.当我执行print(random_number[10][7][8][9]) ,出现错误。 I dont want to print out all the numbers because the first few are almost always the same.我不想打印出所有数字,因为前几个几乎总是相同的。 Thanks for helping.谢谢你的帮助。

i would definetly call those pseudorandom, there is no randomizing involved, because you are just using a clock, which is definetly predictable, although it's hard to predict the microseconds on execution.我会明确地称这些为伪随机,不涉及随机化,因为您只是使用一个时钟,这绝对是可预测的,尽管很难预测执行时的微秒。

Depending on the time your last digits might be 0 and get stripped, that's the reason why you are running into an IndexError exception.根据您的最后一位数字可能为 0 并被剥离的时间,这就是您遇到IndexError异常的原因。

The simple answer is "no": you have not produced either random or pseudo-random numbers.简单的答案是“否”:您没有产生随机数或伪随机数。 Search on line for "random number generation" ... which you should have done before writing your program, let alone posting here.在线搜索“随机数生成”......您应该在编写程序之前完成,更不用说在这里发布了。

The three "point" variables are highly dependent on one another;三个“点”变量相互高度依赖; for all practical purposes, you've taken the present time in seconds, cubed it, and extracted four consecutive digits from the product.出于所有实际目的,您已经以秒为单位计算了当前时间,将其求立方,并从乘积中提取了四个连续的数字。 Since the digits of cubes are not "nicely" distributed, you should find that your do not have a uniform distribution over the ten digits, let alone a pseudo-random sequence.由于立方体的数字不是“很好”分布,您应该会发现您在十位数字上没有均匀分布,更不用说伪随机序列了。

Since the code does not read from any physical source of randomness (a reverse-biased diode, Geiger counter, weather patterns, a lava lamp), it is the result of a deterministic process, and therefore is at best pseudo-random.由于代码不从任何物理随机源(反向偏置二极管、盖革计数器、天气模式、熔岩灯)读取,因此它是确定性过程的结果,因此充其量是伪随机的。 Because the process you chose has no mathematical foundation to explain its distribution or other properties, it is likely to be not pseudo-random either, or very weakly pseudo-random.因为您选择的过程没有数学基础来解释其分布或其他属性,所以它很可能也不是伪随机,或者是非常弱的伪随机。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM