| 1 | import encoding.csv |
| 2 | import flag |
| 3 | import os |
| 4 | import rand |
| 5 | |
| 6 | struct CityMean { |
| 7 | city string |
| 8 | mean f64 |
| 9 | } |
| 10 | |
| 11 | fn main() { |
| 12 | mut fp := flag.new_flag_parser(os.args) |
| 13 | fp.version('1brc sample generator v1.0.0') |
| 14 | fp.skip_executable() |
| 15 | fp.application('Sample generator for 1 billion rows challenge') |
| 16 | fp.description('The 1 billion rows challenge solved in V.\nFor details, see https://www.morling.dev/blog/one-billion-row-challenge/') |
| 17 | input_file := fp.string('city-file', `i`, 'cities.txt', |
| 18 | 'Path to input file with cities and means list') |
| 19 | fp.limit_free_args_to_exactly(1)! |
| 20 | sample_count := fp.remaining_parameters()[0].u64() |
| 21 | |
| 22 | content := os.read_file(input_file) or { panic(err) } |
| 23 | mut reader := csv.new_reader(content, csv.ReaderConfig{ delimiter: `,` }) |
| 24 | mut means := []CityMean{} |
| 25 | for { |
| 26 | rec := reader.read() or { break } |
| 27 | means << CityMean{ |
| 28 | city: rec[0] |
| 29 | mean: rec[1].f64() |
| 30 | } |
| 31 | } |
| 32 | |
| 33 | for _ in 0 .. sample_count / 2 { |
| 34 | mut city := rand.intn(means.len)! |
| 35 | m1, m2 := rand.normal_pair(mu: means[city].mean, sigma: 10)! |
| 36 | println('${means[city].city};${m1:.1f}') |
| 37 | city = rand.intn(means.len)! |
| 38 | println('${means[city].city};${m2:.1f}') |
| 39 | } |
| 40 | } |
| 41 | |