Created
October 2, 2012 03:58
-
-
Save mungruby/3816082 to your computer and use it in GitHub Desktop.
Generate ascii strings
This file contains 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 -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