Last active
December 19, 2016 22:39
-
-
Save pkarman/271e7aaab290a04116cab53540a86f3a to your computer and use it in GitHub Desktop.
ruby random phrase generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| num_words = (ARGV[0] || 16).to_i | |
| file = '/usr/share/dict/words' | |
| total_words = `wc -l #{file}` | |
| random_line_nums = Array.new(num_words * 5) { rand(1...total_words.to_i) } | |
| words = [] | |
| File.open(file) do |fh| | |
| fh.each_with_index do |line, line_num| | |
| next unless random_line_nums.include?(line_num) | |
| words << line.chomp | |
| end | |
| end | |
| words.shuffle! | |
| phrase = words.select {|word| word.length < 10 && word.length > 2 }.sample(num_words) | |
| puts "picked words: #{phrase.inspect}" |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage: