Probability of collisions

Suppose you have a hash table with M slots or buckets, and you have N keys to randomly insert into it

What is the probability that there will be a collision among these keys?

You might think that as long as the table is less than half full, there is less than 50% chance of a collision, but this is not true

The probability of at least one collision among N random independently inserted keys is

The probability that the ith key will not collide with any of the i1 keys already in the table is just the probability that it will land in one of the Mi1 available empty locations. If all locations are equally likely, then the probability of at least one collision when inserting N keys in a table of size M is:
CONTENTS PREVIOUS NEXT