Skip to content

Instantly share code, notes, and snippets.

@hunj

hunj/gen.rb

Created Mar 6, 2016
Embed
What would you like to do?
Generate random numbers
require 'csv'
# Data generation
@data = {}
(0..100).each do |i|
@data[i] = {
:age => Random.rand(18...80),
:gender => Random.rand(0..1),
:prescription_history => []
}
@data[i][:prescription_history] << 0
(0..Random.rand(1...10)).each do |j|
@data[i][:prescription_history] << Random.rand(-15...15)
end
end # end Data generation
# filters the data with given age range
def filter_by_age_range age_range
arr = []
@data.values.each do |point|
if age_range.include? point[:age]
arr.push point
end
end
arr
end
def filter_by_gender gender_num #0 for male, 1 for female
arr = []
@data.values.each do |point|
if gender_num == point[:gender]
arr.push point
end
end
arr
end
# put that into a file
column_names = @data[0].keys
s = CSV.generate do |csv|
csv << column_names
@data.values.each do |x|
csv << x.values.flatten
end
end
File.write("result.csv", s)
'''
@data.each do |point|
p point
end
'''
'''try it out
puts filter_by_age_range (10..25)
puts filter_by_gender 1
'''
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment