Created
September 24, 2018 15:55
-
-
Save gnomus/b90689eded80fc5addd88f705683e277 to your computer and use it in GitHub Desktop.
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
require 'set' | |
require 'csv' | |
CUSTOMER_AMOUNT = 20 | |
CUSTOMER_AMOUNT_FOCUS_HH = 50 | |
CUSTOMER_AMOUNT_FOCUS_MU = 30 | |
CUSTOMER_AMOUNT_FOCUS_BE = 70 | |
PRODUCT_AMOUNT = 10 | |
def uniq_rand_n(n, min, max) | |
raise "Uniq Rand N to big" if n > (max-min) | |
randoms = Set.new | |
loop do | |
r = rand(max-min) + min | |
randoms << r | |
return randoms.to_a if randoms.size >= n | |
end | |
end | |
def choose_weighted(weighted) | |
sum = weighted.inject(0) do |sum, item_and_weight| | |
sum += item_and_weight[1] | |
end | |
target = rand(sum) | |
weighted.each do |item, weight| | |
return item if target <= weight | |
target -= weight | |
end | |
end | |
customers = [] | |
first_names = CSV.read("first_names.csv") | |
last_names = CSV.read("last_names.csv") | |
strassen = CSV.read("strassen.csv") | |
plz_einwohner = CSV.read("plz_einwohner.csv").map do |r| | |
[r[0].to_i,r[1].to_i] | |
end | |
plz_focus_hh = plz_einwohner.select do |r| | |
(r[0] >= 20095 && r[0] <= 22769) | |
end | |
plz_focus_mu = plz_einwohner.select do |r| | |
(r[0] >= 80331 && r[0] <= 81929) | |
end | |
plz_focus_be = plz_einwohner.select do |r| | |
(r[0] >= 10115 && r[0] <= 14199) | |
end | |
uniq_rand_n(CUSTOMER_AMOUNT, 1000000, 1999999).each do |id| | |
customers << [ | |
id, | |
first_names[rand(first_names.length)], | |
last_names[rand(last_names.length)], | |
"#{strassen[rand(last_names.length)].first} #{rand(132)}", | |
choose_weighted(plz_einwohner) | |
] | |
end | |
uniq_rand_n(CUSTOMER_AMOUNT_FOCUS_HH, 2000000, 2999999).each do |id| | |
customers << [ | |
id, | |
first_names[rand(first_names.length)], | |
last_names[rand(last_names.length)], | |
"#{strassen[rand(last_names.length)].first} #{rand(132)}", | |
choose_weighted(plz_focus_hh) | |
] | |
end | |
uniq_rand_n(CUSTOMER_AMOUNT_FOCUS_MU, 2000000, 2999999).each do |id| | |
customers << [ | |
id, | |
first_names[rand(first_names.length)], | |
last_names[rand(last_names.length)], | |
"#{strassen[rand(last_names.length)].first} #{rand(132)}", | |
choose_weighted(plz_focus_mu) | |
] | |
end | |
uniq_rand_n(CUSTOMER_AMOUNT_FOCUS_BE, 2000000, 2999999).each do |id| | |
customers << [ | |
id, | |
first_names[rand(first_names.length)], | |
last_names[rand(last_names.length)], | |
"#{strassen[rand(last_names.length)].first} #{rand(132)}", | |
choose_weighted(plz_focus_be) | |
] | |
end | |
puts "KundenId,Vorname,Nachname,Strasse,PLZ" | |
customers.each do |c| | |
puts c.join(",") | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment