Skip to content

Instantly share code, notes, and snippets.

@tmountain
Created November 15, 2018 22:16
Show Gist options
  • Save tmountain/08552106bc0e9b29b0d64e968a5976be to your computer and use it in GitHub Desktop.
Save tmountain/08552106bc0e9b29b0d64e968a5976be to your computer and use it in GitHub Desktop.
Generates a table of farmers/cows given a file with one name per line.
#!/usr/bin/ruby
fh = File.new('names.csv', 'r')
names = []
while line = fh.gets
line.chomp!
names << line
end
names = names.shuffle
farmers = names[0, 75]
cows = names[75, names.count]
def rand_thingy(thingy)
count = thingy.count
idx = rand(0..count)
thingy[idx]
end
ffile = File.new('fdata.csv', 'w')
findex = {}
farmers.each_with_index do |farmer, index|
findex[farmer] = index
ffile.puts "#{index},#{farmer}"
end
ffile.close
cindex = {}
cows.each_with_index do |cow, index|
cindex[cow] = index
end
cfile = File.new('cdata.csv', 'w')
while !cows.empty?
farmer = rand_thingy(farmers)
cow = cows.pop
cow_id = cindex[cow]
farmer_id = findex[farmer]
cfile.puts "#{cow_id},#{farmer_id},#{cow}"
end
cfile.close
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment