Skip to content

Instantly share code, notes, and snippets.

@mungruby
Created October 2, 2012 03:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mungruby/3816082 to your computer and use it in GitHub Desktop.
Save mungruby/3816082 to your computer and use it in GitHub Desktop.
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