随机生成的密码中出现 3 个字符的字符串的概率

发布于 2024-11-26 00:39:33 字数 155 浏览 1 评论 0原文

如果您有一个随机生成的密码,仅包含字母数字字符,长度为 12,并且比较不区分大小写(即“A”==“a”),则长度为 3 的一个特定字符串(例如“ ABC') 会出现在该密码中吗?

我知道可能的组合总数是 (26+10)^12,但除此之外,我有点迷失了。对数学的解释也是最有帮助的。

If you have a randomly generated password, consisting of only alphanumeric characters, of length 12, and the comparison is case insensitive (i.e. 'A' == 'a'), what is the probability that one specific string of length 3 (e.g. 'ABC') will appear in that password?

I know the number of total possible combinations is (26+10)^12, but beyond that, I'm a little lost. An explanation of the math would also be most helpful.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

番薯 2024-12-03 00:39:33

字符串“abc”可以出现在第一个位置,使字符串看起来像这样:

abcXXXXXXXXX

...其中 X 可以是任何字母或数字。这样的字符串共有 (26 + 10)^9 个。

它可以出现在第二个位置,使字符串看起来像:

XabcXXXXXXXX

并且也有 (26 + 10)^9 个这样的字符串。

由于“abc”可以出现在从第一个到第十个位置的任何位置,因此这样的字符串有 10*36^9 个。

但这会超出计数,因为它会计算(例如)这样的字符串两次:

abcXXXabcXXX

所以我们需要计算所有这样的字符串,并将它们从总数中减去。

由于此模式中有 6 个 X,因此有 36^6 个字符串与此模式匹配。

我得到 7+6+5+4+3+2+1 = 28 个这样的模式。 (如果第一个“abc”在开头,则第二个可以在 7 个位置中的任意位置。如果第一个“abc”在第二个位置,则第二个可以在 6 个位置中的任意位置。依此类推。

)减去 28*36^6。

...但这减去了太多,因为它减去了这样的字符串三次,而不是一次:

abcXabcXabcX

所以我们必须像这样的字符串添加两次。我得到 4+3+2+1 + 3+2+1 + 2+1 + 1 = 20 个这样的模式,这意味着我们必须添加 2*20*(36^3)。

但是数学计算了这个字符串四次:

abcabcabcabc

...所以我们必须减去 3。

最终答案:

10*36^9 - 28*36^6 + 2*20*(36^3) - 3

将其除以 36^12 以获得概率。

另请参阅包含-排除原则。如果我的计数有误,请告诉我。

The string "abc" can appear in the first position, making the string look like this:

abcXXXXXXXXX

...where the X's can be any letter or number. There are (26 + 10)^9 such strings.

It can appear in the second position, making the string look like:

XabcXXXXXXXX

And there are (26 + 10)^9 such strings also.

Since "abc" can appear at anywhere from the first through 10th positions, there are 10*36^9 such strings.

But this overcounts, because it counts (for instance) strings like this twice:

abcXXXabcXXX

So we need to count all of the strings like this and subtract them off of our total.

Since there are 6 X's in this pattern, there are 36^6 strings that match this pattern.

I get 7+6+5+4+3+2+1 = 28 patterns like this. (If the first "abc" is at the beginning, the second can be in any of 7 places. If the first "abc" is in the second place, the second can be in any of 6 places. And so on.)

So subtract off 28*36^6.

...but that subtracts off too much, because it subtracted off strings like this three times instead of just once:

abcXabcXabcX

So we have to add back in the strings like this, twice. I get 4+3+2+1 + 3+2+1 + 2+1 + 1 = 20 of these patterns, meaning we have to add back in 2*20*(36^3).

But that math counted this string four times:

abcabcabcabc

...so we have to subtract off 3.

Final answer:

10*36^9 - 28*36^6 + 2*20*(36^3) - 3

Divide that by 36^12 to get your probability.

See also the Inclusion-Exclusion Principle. And let me know if I made an error in my counting.

几味少女 2024-12-03 00:39:33

如果A不等于C,则ABC出现在长度为字符串中的概率P(n) n (假设每个字母数字符号的可能性相同)

P(n)=P(n-1)+P(3)[1-P(n-3)]

P(0)=P(1)=P(2)=0 and P(3)=1/(36)^3

If A is not equal to C, the probability P(n) of ABC occuring in a string of length n (assuming every alphanumeric symbol is equally likely) is

P(n)=P(n-1)+P(3)[1-P(n-3)]

where

P(0)=P(1)=P(2)=0 and P(3)=1/(36)^3
吃素的狼 2024-12-03 00:39:33

扩展 Paul R 的答案。概率(对于同样可能的结果)是事件可能结果的数量除以可能结果的总数。

在长度为 12 的字符串中,有 10 个可能的位置可以找到长度为 3 的字符串。还有 9 个位置可以用任何其他字母数字字符填充,这导致 36^9的可能性。因此,您的事件的可能结果数为 10 * 36^9

将其除以结果总数 36^12。你的答案是 10 * 36^-3 = 0.000214

编辑:这并不完全正确。在此解决方案中,某些情况会被重复计算。然而,它们对概率的贡献非常小,因此这个答案在小数点后 11 位内仍然正确。如果您想要完整的答案,请参阅 Nemo 的答案。

To expand on Paul R's answer. Probability (for equally likely outcomes) is the number of possible outcomes of your event divided by the total number of possible outcomes.

There are 10 possible places where a string of length 3 can be found in a string of length 12. And there are 9 more spots that can be filled with any other alphanumeric characters, which leads to 36^9 possibilities. So the number of possible outcomes of your event is 10 * 36^9.

Divide that by your total number of outcomes 36^12. And your answer is 10 * 36^-3 = 0.000214

EDIT: This is not completely correct. In this solution, some cases are double counted. However they only form a very small contribution to the probability so this answer is still correct up to 11 decimal places. If you want the full answer, see Nemo's answer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文