Skip to content

Instantly share code, notes, and snippets.

@pkarman
Last active December 19, 2016 22:39
Show Gist options
  • Select an option

  • Save pkarman/271e7aaab290a04116cab53540a86f3a to your computer and use it in GitHub Desktop.

Select an option

Save pkarman/271e7aaab290a04116cab53540a86f3a to your computer and use it in GitHub Desktop.
ruby random phrase generator
#!/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}"
@pkarman
Copy link
Copy Markdown
Author

pkarman commented Dec 19, 2016

Example usage:

# generate a 12 word phrase
% ruby rander.rb 12

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment