fork download
  1. #!/usr/bin/env perl
  2. use warnings;
  3. use strict;
  4. use List::Util qw(shuffle);
  5.  
  6. # freq table extracted from Ubuntu's /usr/share/dict/words wordlist
  7. my %freq = (
  8. a => 64439,
  9. b => 15526,
  10. c => 31872,
  11. d => 28531,
  12. e => 88833,
  13. f => 10675,
  14. g => 22712,
  15. h => 19320,
  16. i => 66986,
  17. j => 1948,
  18. k => 8409,
  19. l => 41107,
  20. m => 22508,
  21. n => 57144,
  22. o => 48944,
  23. p => 22274,
  24. q => 1524,
  25. r => 57347,
  26. s => 90113,
  27. t => 53006,
  28. u => 26118,
  29. v => 7989,
  30. w => 7530,
  31. x => 2124,
  32. y => 12652,
  33. z => 3281,
  34. );
  35.  
  36. my @letters = sort keys %freq;
  37.  
  38. my $sum = 0;
  39. my %running_sum;
  40. for(@letters) {
  41. $running_sum{$_} = $sum;
  42. $sum += $freq{$_};
  43. }
  44.  
  45. my $curmax = 1;
  46. my $curletter = $#letters;
  47. my $i = 100; # the number of letters we want to generate
  48. my @result;
  49. while ($i > 0) {
  50. # curmax generates a uniformly distributed decreasing random number in [0,1)
  51. # see http://repository.cmu.edu/cgi/viewcontent.cgi?article=3483&context=compsci
  52. $curmax = $curmax * (1-rand())**(1. / $i);
  53.  
  54. # scale the random number to [0,$sum)
  55. my $num = int ($curmax * $sum);
  56.  
  57. # find the range that contains $num
  58. while ($num < $running_sum{$letters[$curletter]}) {
  59. $curletter--;
  60. }
  61.  
  62. push(@result, $letters[$curletter]);
  63.  
  64. $i--;
  65. }
  66.  
  67. # since $result is sorted, you may want to use shuffle it first
  68. print "", join('', shuffle(@result));
  69.  
Success #stdin #stdout 0s 3900KB
stdin
Standard input is empty
stdout
titctntrfesreisttalieirgeipaeiaueaeadfxlxsehgaconinisucfprtinroeeblolfoelnejrsewrunnlenihsdodaeytapi