Hulihan Applications
about projects portfolio services contact_us

Viewing Posts from October 2009


Ruby Random Data Generator

    Here's a quick ruby script that will generate a file full of random data. You may be wondering...why would anyone want a bunch of random numbers? Well, we use this kind of data to make sample graphs in different programs and to simulate data results that a particular piece of hardware should be reporting(like data a sensor would report). We often write software for scientific devices that spew forth tons of data. This script generates example data that such devices might spew, so we know how to handle it on the software side of things.

This is a simple ruby script that asks the user for a couple specifications for the data that will be generated. It asks the user for the amount of numbers to generate, the range of values to generate from, the filename to store the data in, and how it should be delimited(comma or newline).

Here's the code:
#!/usr/bin/ruby

# Random Data Generator 
# Author: Hulihan Applications
# Purpose: Uh, To generate random data 
  
def get_keyboard_input(options = {})
  options[:default] ||= "default" # set options[:default]'s  default 
  input = STDIN.gets
  input = input.chomp
  input = options[:default] if (input.size == 0 && input.to_i == 0) # return the default value if user pressed enter without entering anything.
  return input 
end

# This script generates random data for graphing function. Delimiting options: comma, newline
puts "Generating Random Data...\n"

puts "How Many Numbers do you want to generate?(default is 100)"
@number = get_keyboard_input(:default => 100)

puts "Number values will be generated randomly What is the Minimum value?(default is 0)"
@min_value = get_keyboard_input(:default => 0)

puts "Number values will be generated randomly What is the Minimum value?(default is 100)"
@max_value = get_keyboard_input(:default => 100)

puts "What is the filename to store the data in?(default is data.txt, if file exists, data will be concatenated at EOF)"
@filename = get_keyboard_input(:default => "data.txt")

puts "What kind of delimiter would you like, comma or newline?(default is newline)"
@delimiter = get_keyboard_input(:default => "newline")

puts "Add tab-delimited ids, yes or no?(default is no)"
@add_ids = get_keyboard_input(:default => "no")

puts "Generating #{@number.to_i} numbers between #{@min_value} and #{@max_value}..."

file = File.new(@filename, "a+")

# generate data
for i in 1..@number.to_i
  if @add_ids.downcase == "yes" || @add_ids.downcase == "y" # add tab-delimited ids
    file.print "#{i}\t"
  end
  file.print rand((@max_value.to_i + 1) - @min_value.to_i) + (@min_value.to_i) # write the random data from a range. We add 1 to @max_value because rand(100) would only give up to 99
  # write delimiter 
  if @delimiter.downcase == "newline"
     file.print "\n"
  elsif @delimiter.downcase == "comma"
     file.print "," unless i == @number.to_i # add comma unless last value in file
  end
end

puts "File: #{@filename} saved successfully!"
file.close
This script should be ran from command line(in windows, mac os, or linux). Here's an sample of what it looks like when it's running:
$ ruby generate_random_data.rb


Generating Random Data...
How Many Numbers do you want to generate?(default is 100)
1000
Number values will be generated randomly What is the Minimum value?(default is 0)
55
Number values will be generated randomly What is the Minimum value?(default is 100)
60
What is the filename to store the data in?(default is data.txt, if file exists, data will be concatenated at EOF)
my_random_data.txt
What kind of delimiter would you like, comma or newline?(default is newline)
comma
Add tab-delimited ids, yes or no?(default is no)
no
Generating 1000 numbers between 55 and 60...
File: my_random_data.txt saved successfully!
I just thought i'd share, as this may be useful for people that need any amount of random data to use.

  0 Comments
Tags:


Converting a String to a Class in Ruby

    

In Ruby, you can easily convert a string, or any kind of text, into a Class. Here's a quick example:

class_name = "new_class" # set string with name of class, will camelize to NewClass 
new_object = class_name.camelize.constantize.new # call NewClass.new

  0 Comments
Tags:




Hulihan Applications © 2007-2009
No portion of this site may be copied, altered, duplicated or otherwise used without the express written approval of Hulihan Applications.