What’s the formula for finding the possible number of combinations in this example: Generate 4 characters from A-Z and 0-9 but they will be alternating. Ex: L7W8, Q6N6, H3P1, etc..
To illustrate in PHP code:
$length = 4;
$pool_1 = explode(',', 'A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z');
$pool_2 = explode(',', '1,2,3,4,5,6,7,8,9,0');
$s = '';
for ($i = 0; $i < $length; $i++)
$s.= ($i % 2) ? $pool_2[array_rand($pool_2)] : $pool_1[array_rand($pool_1)];
echo $s;
If $length is 4, what’s the formula to get the possible number of combinations?
The answer is
26 * 10 * 26 * 10forlength = 4Explanation:
for first position, you got 26 choices. for second, you got 10. possible variations : 26 * 10
for third position, you got 26 choices again. so you will get possible variations : ( 26 * 10 ) * 26
and so on..
This is basic combinatorics. Picking 1 out of 26 is indicated mathematically as
26C1which equals26/1 = 26.NCR = (N.N-1.N-2 .. N-R-1)/(1.2.3 .. R)