简体   繁体   English

如何在 Python 随机性的限制下创造一个真正不可能的条件?

[英]How to make a truly improbable condition with limitations of Python's random?

So I am trying to find a way to get a massively improbable condition based on random generations.所以我试图找到一种方法来获得基于随机生成的大量不可能的条件。 To better explain, here's an example:为了更好地解释,这里有一个例子:

from random import *
import ctypes

random1 = [randint(0, 2 ** 32 - 1) for j in range(10000000)]

while True:
    random2 = [randint(0, 2 ** 32 - 1) for i in range(10000000)]
    if set(random2) == set(random1):
        MessageBox = ctypes.windll.user32.MessageBoxW
        MessageBox(None, 'Match', 'Output', 0)
        break

Because of the limitations and functionality of mersene twister, and the uniformity of its distribution of numbers, it's quite likely we will generate 10 milion numbers in both lists, where when order doesn't matter and duplicates are removed, they will match quite often.由于 mersene twister 的局限性和功能,以及其数字分布的均匀性,我们很可能会在两个列表中生成 1000 万个数字,当顺序无关紧要并删除重复项时,它们会经常匹配。

This is not that rare, but the following code is slightly better:这种情况并不少见,但下面的代码稍微好一些:

from random import *
import ctypes

while True:
    if random() == 0.0:
        MessageBox = ctypes.windll.user32.MessageBoxW
        MessageBox(None, 'Match', 'Output', 0)
        break

This is a lot less likely to happen, but with massive single core performance, quite common today, it's still easily likely to get a match in 1~ day.这种情况发生的可能性要小得多,但是在今天很常见的大量单核性能下,它仍然很容易在 1~ 天内得到匹配。 The probability being 1/2^56, and with the limitations of mersenne twister in mind, it's not that unlikely to happen.概率为 1/2^56,并且考虑到 mersenne twister 的限制,这不太可能发生。

Is there a good way to write a condition utilizing randomness in python that will truly be extremely unlikely to happen?.. That would say, take a year to crack, or more.有没有一种好方法可以利用 python 中的随机性来编写一个条件,这种情况真的极不可能发生?...也就是说,需要一年或更长时间才能破解。

Alternatively I thought to turn to hash matching... creating a random SHA256 hash, then generating random big data, and hashing it through sha256 to try and match the hash.或者,我想转向 hash 匹配...创建一个随机 SHA256 hash,然后生成随机大数据,并通过 sha256 对其进行散列以尝试匹配 Z0800FC577294C34E0B28AD2839 But I don't know a way to observe probability in that case.但我不知道在这种情况下观察概率的方法。

You may be interested in the geometric distribution , which counts the number of failures before the first success (some works say it counts that number plus the first success instead).您可能对几何分布感兴趣,它计算第一次成功之前的失败次数(有些作品说它计算的是那个数字加上第一次成功)。 As an example, the probability of getting no failures in a row is 1/2, one failure in a row is 1/4, two in a row is 1/8, three in a row is 1/16, and so on.例如,连续没有失败的概率是 1/2,连续失败的概率是 1/4,连续失败的概率是 1/8,连续失败的概率是 1/16,以此类推。 If we take a zero-bit to mean failure and a one-bit to mean success, that means that with more zero-bits, it becomes less probable for that many zero-bits to be generated at random.如果我们将零位表示失败,一位表示成功,这意味着零位越多,随机生成那么多零位的可能性就越小。 As an example of an "improbable event", you can treat 30 or more zero-bits in a row as improbable.作为“不可能事件”的示例,您可以将连续 30 个或更多零位视为不可能事件。

Mersenne Twister and pseudorandom number generators (PRNGs) in general have cycles. Mersenne Twister 和伪随机数生成器 (PRNG) 通常具有循环。 The size of this cycle affects how many zero-bits the PRNG can generate in a row.这个周期的大小会影响 PRNG 可以连续生成多少个零位。 For example, Mersenne Twister has a cycle of 2^19937 - 1 numbers, so that in theory, it can cycle through all states except for the all-zeros state.例如,Mersenne Twister 有一个 2^19937 - 1 个数字的循环,因此理论上它可以循环通过除全零 state 之外的所有状态。 Thus, it can generate no more than 19937 * 2 zero-bits in a row.因此,它可以连续生成不超过19937 * 2零位。 (This is if we treat Mersenne Twister as outputting individual bits, rather than 32 bits, at a time.) (如果我们将 Mersenne Twister 视为一次输出单个位,而不是 32 位。)

This is in contrast to nondeterministic random number generators (RNGs), which don't have cycles but still generate random-behaving numbers.这与非确定性随机数生成器(RNG) 形成对比,后者没有循环但仍会生成随机行为数。 If the numbers it generates are independent, uniform, and random bits, then there is no telling how many zero-bits the RNG can randomly generate at most.如果它生成的数字是独立的、统一的和随机的位,那么不知道 RNG 最多可以随机生成多少个零位。 One example of an RNG that uses nondeterminism is found in Python's secrets module, specifically secrets.randbelow() .在 Python 的secrets模块中可以找到一个使用非确定性的 RNG 示例,特别是secrets.randbelow() (In practice, this module is likely to use a PRNG, but may gather "entropy" from nondeterministic sources from time to time, so that in practice the module's RNG is nondeterministic.) (在实践中,该模块可能使用 PRNG,但可能不时从非确定性来源收集“熵”,因此在实践中该模块的 RNG 是非确定性的。)

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

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