Skip to content

@mungruby /random_strings.rb
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Generate ascii strings
#!/usr/bin/env ruby -w
class BitString
attr_accessor :bit_string
def initialize string_length
@bit_string = random_bit_string string_length
end
def random_bit_string length
arr = Array.new length
length.times {|i| arr[i] = random_bit}
arr.join
end
def random_bit
rand(100) % 2
end
def to_ascii
arr = []
bit_string.scan(/[01]{7}/).each do |str|
arr << str.to_i(2).chr if str.length == 7
end
arr.join
end
end
class RandomStrings
attr_accessor :str_length, :str_count
def initialize string_length, string_count
@str_length = string_length
@str_count = string_count
end
def run
str_count.times do
p BitString.new(str_length).to_ascii
end
end
end
if __FILE__ == $0
length_of_string = ARGV[0].to_i
number_of_strings = ARGV[1].to_i
RandomStrings.new(length_of_string, number_of_strings).run
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.