Suppose there is a scenario:
Assign a random code (number/Letter) to each person entering the room. The random code is valid for a period of time, to keep the probability of two people assigning the same random code below the specified value, the minimum length of the random code must be several characters?
I calculated it like this:
Settings:
Concurrency: P(For example, 100/s)
Cache Time: T(E.g. 1800 s)
Collision rate: R(For example, 1 ‰)
Capacity: c
Then:
C = p * T/R
The above test cases are substituted into computing:
C = 100*1800*1000 = 180000000 (0.18 billion)
That is to say, the total number of random character strings must be greater than 0.18 billion, so the number of digits is enough.
There are 10 possibilities for numbers, 26 possibilities for letters, and 36 possibilities for numbers and letters.
You can directly refer to the following table (approximate value ):
Length |
Number |
Letter |
Numbers & letters |
Numbers & letters (uppercase/lowercase) |
4 |
10 thousand |
0.46 million |
1.68 million |
14.78 million |
5 |
0.1 million |
1.2 million |
60 million |
0.9 billion |
6 |
1 million |
0.3 billion |
2.2 billion |
56.8 billion |
7 |
10 million |
8 billion |
78 billion |
3.52 trillion |
8 |
0.1 billion |
200 billion |
2.8 trillion |
218 trillion |
9 |
1 billion |
5.4 trillion |
100 trillion |
13.5 thousand trillion |
10 |
10 billion |
141 trillion |
3656 trillion |
0.84 million trillion |
You can only select a condition greater than 0.18 billion, that is:
Only numbers: 9 Characters
Only letters: 6 Characters
Number + letter: 6 Characters
Theoretically, the actual collision rate will gradually increase with the increase of time, but because of the validity period of the random code, this value will always be lower than R
According to the actual test results, it is basically in line with the theoretical expectations.
This method is applicable to scenarios where random IDs are required. We will not talk much about the specific application scenarios :)
----------------------------------------- 2013-5-3 update -------------------------------------------
Here we will talk about the collision rate R:
If P = 100, r = 1%, a collision occurs every second;
If P = 100, r = 1‰, a collision occurs in 10 seconds;
If P = 1000, r = 1%, 10 collisions occur per second;
......
The value of R can be used as a reference.