随机生成的密码中出现 3 个字符的字符串的概率
如果您有一个随机生成的密码,仅包含字母数字字符,长度为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
字符串“abc”可以出现在第一个位置,使字符串看起来像这样:
...其中 X 可以是任何字母或数字。这样的字符串共有 (26 + 10)^9 个。
它可以出现在第二个位置,使字符串看起来像:
并且也有 (26 + 10)^9 个这样的字符串。
由于“abc”可以出现在从第一个到第十个位置的任何位置,因此这样的字符串有 10*36^9 个。
但这会超出计数,因为它会计算(例如)这样的字符串两次:
所以我们需要计算所有这样的字符串,并将它们从总数中减去。
由于此模式中有 6 个 X,因此有 36^6 个字符串与此模式匹配。
我得到 7+6+5+4+3+2+1 = 28 个这样的模式。 (如果第一个“abc”在开头,则第二个可以在 7 个位置中的任意位置。如果第一个“abc”在第二个位置,则第二个可以在 6 个位置中的任意位置。依此类推。
)减去 28*36^6。
...但这减去了太多,因为它减去了这样的字符串三次,而不是一次:
所以我们必须像这样的字符串添加两次。我得到 4+3+2+1 + 3+2+1 + 2+1 + 1 = 20 个这样的模式,这意味着我们必须添加 2*20*(36^3)。
但是数学计算了这个字符串四次:
...所以我们必须减去 3。
最终答案:
将其除以 36^12 以获得概率。
另请参阅包含-排除原则。如果我的计数有误,请告诉我。
The string "abc" can appear in the first position, making the string look like this:
...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:
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:
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:
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:
...so we have to subtract off 3.
Final answer:
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.
如果
A
不等于C
,则ABC
出现在长度为字符串中的概率P(n)
n
(假设每个字母数字符号的可能性相同)是
If
A
is not equal toC
, the probabilityP(n)
ofABC
occuring in a string of lengthn
(assuming every alphanumeric symbol is equally likely) iswhere
扩展 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 is10 * 36^9
.Divide that by your total number of outcomes
36^12
. And your answer is10 * 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.