Mathematics Stack Exchange is a question and answer site for people studying math at any level and professionals in related fields. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

We are using character strings (out of 64 possible characters) to identify unique elements in a 'bag' of data. each bag has on average 20 elements, but up to 300.

I am trying to determine what size should that string be so that the probability of a collision (if we pick the characters randomly) is less than 1 in a 1,000,000 for 20 elements, and then for 300 elements.

share|cite|improve this question

From Wikipedia (, a one in a million chance to collide is reached very quickly; with only 32 bits (a 4-byte integer value in most languages), 93 elements have a one in a million chance to collide. You can Base-64 encode this integer using your alphabet. With 64 bits (a "long" in most languages), the number of hashes at which there's a one-in-a-million chance increases to 6.1 million.

share|cite|improve this answer

As an approximate answer, if you have a bag of $20$ elements, there are $\frac {20\cdot 19}2=190$ pairs. If the probability of a match given one pair is $p$, the probability of no match in one pair is $(1-p)$ and the probability of no match in $20$ elements is $(1-p)^{190}$. For very small collision probabilities (like you want) $(1-p)^{190} \approx 1-190p$, so you need $190p \lt 10^{-6}$ or (rounding) $p \lt 5 \cdot 10^{-9}$. Then if your string is $n$ characters long, you need $64^{-n} \lt 5 \cdot 10^{-9}$ Again rounding down and taking base two logs, $-6n \lt -29$, so $n=5$ will work. For $300$, you have about $15^2=225$ times as many pairs, so need two more characters for $7$. With all the rounding down, $6$ might suffice, but how much does one more cost?

share|cite|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.