fork(1) download
  1. <?php
  2.  
  3. // Length of a randomly generated String
  4. $string_length = 9;
  5.  
  6. // Number of strings that will be generated
  7. $tries = 3000000;
  8.  
  9. // Arbitrary pool of characters
  10. // These will be used to generate the string
  11. $character_pool = '1234567890abcdefghijklmnopqrstuvwxyz';
  12. $pool_size = strlen($character_pool);
  13.  
  14. // Maximum number of combinations
  15. // Given the length of strings and available characters
  16. $combination_count = pow($pool_size, $string_length);
  17.  
  18. echo "With a character pool of $pool_size characters you can generate $combination_count unique strings of $string_length characters long.\n";
  19.  
  20. /**
  21.  * Secondary Consideration
  22.  *
  23.  * The probability of any one of a number of generations ($tries) matching a
  24.  * previously generated value.
  25.  *
  26.  * This only returns 100%, am I missing something?
  27.  */
  28.  
  29. $probability = pow(1 - 1/$combination_count, $tries);
  30.  
  31. echo "The probabiity of generating a match with a previously generated string after $tries tries is ".round($probability*100,2)."%.\n";
Success #stdin #stdout 0.02s 24448KB
stdin
Standard input is empty
stdout
With a character pool of 36 characters you can generate 1.0155995666842E+14 unique strings of 9 characters long.
The probabiity of generating a match with a previously generated string after 3000000 tries is 100%.