Creating a word from a random set of specific letters
I'm trying to generate a word with a specific set of letters.
The first letter of the word must contain the letters P,G,T,K the sec开发者_JAVA技巧ond A,E,I,O,U
I have tried generating them with the code blow with no success. Once I store all of these random letters as variables I will then join them together to make the randomized word
letter1 = rand(80,84,75,71).chr
letter2 = rand(97,101,105,111,117,121).chr
name = letter1 + letter2 + letter2 + letter1 + letter2
puts name
puts 'PGTK'[rand(4), 1] + 'AEIOU'[rand(5), 1]
For some real fun, use actual noise-derived entropy:
def noise_index s
s[@f.sysread(1).unpack('C').first/256.0*s.length, 1]
end
def run
open '/dev/random', 'r' do |f|
@f = f
100.times do
puts noise_index('PGTK') + noise_index('AEIOU')
end
end
end
If you need just a final word of two letters:
['PGTK', 'AEIOU'].map { |s| s.chars.to_a.sample }.join
Or following the example in your question:
letter1, letter2 = ['PGTK', 'AEIOU'].map { |s| s.chars.to_a.sample }
name = letter1 + letter2 + letter2 + letter1 + letter2
精彩评论