github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/faker/name.go (about) 1 // Copyright 2019 The Cockroach Authors. 2 // 3 // Use of this software is governed by the Business Source License 4 // included in the file licenses/BSL.txt. 5 // 6 // As of the Change Date specified in that file, in accordance with 7 // the Business Source License, use of this software will be governed 8 // by the Apache License, Version 2.0, included in the file 9 // licenses/APL.txt. 10 11 package faker 12 13 import ( 14 "fmt" 15 16 "golang.org/x/exp/rand" 17 ) 18 19 type nameFaker struct { 20 formatsFemale, formatsMale *weightedEntries 21 firstNameFemale, firstNameMale *weightedEntries 22 lastName *weightedEntries 23 prefixFemale, prefixMale *weightedEntries 24 suffixFemale, suffixMale *weightedEntries 25 } 26 27 // Name returns a random en_US person name. 28 func (f *nameFaker) Name(rng *rand.Rand) string { 29 if rng.Intn(2) == 0 { 30 return f.formatsFemale.Rand(rng).(func(rng *rand.Rand) string)(rng) 31 } 32 return f.formatsMale.Rand(rng).(func(rng *rand.Rand) string)(rng) 33 } 34 35 func newNameFaker() nameFaker { 36 f := nameFaker{} 37 f.formatsFemale = makeWeightedEntries( 38 func(rng *rand.Rand) string { 39 return fmt.Sprintf(`%s %s`, f.firstNameFemale.Rand(rng), f.lastName.Rand(rng)) 40 }, 0.97, 41 func(rng *rand.Rand) string { 42 return fmt.Sprintf(`%s %s %s`, f.prefixFemale.Rand(rng), f.firstNameFemale.Rand(rng), f.lastName.Rand(rng)) 43 }, 0.015, 44 func(rng *rand.Rand) string { 45 return fmt.Sprintf(`%s %s %s`, f.firstNameFemale.Rand(rng), f.lastName.Rand(rng), f.suffixFemale.Rand(rng)) 46 }, 0.02, 47 func(rng *rand.Rand) string { 48 return fmt.Sprintf(`%s %s %s %s`, f.prefixFemale.Rand(rng), f.firstNameFemale.Rand(rng), f.lastName.Rand(rng), f.suffixFemale.Rand(rng)) 49 }, 0.005, 50 ) 51 52 f.formatsMale = makeWeightedEntries( 53 func(rng *rand.Rand) string { 54 return fmt.Sprintf(`%s %s`, f.firstNameMale.Rand(rng), f.lastName.Rand(rng)) 55 }, 0.97, 56 func(rng *rand.Rand) string { 57 return fmt.Sprintf(`%s %s %s`, f.prefixMale.Rand(rng), f.firstNameMale.Rand(rng), f.lastName.Rand(rng)) 58 }, 0.015, 59 func(rng *rand.Rand) string { 60 return fmt.Sprintf(`%s %s %s`, f.firstNameMale.Rand(rng), f.lastName.Rand(rng), f.suffixMale.Rand(rng)) 61 }, 0.02, 62 func(rng *rand.Rand) string { 63 return fmt.Sprintf(`%s %s %s %s`, f.prefixMale.Rand(rng), f.firstNameMale.Rand(rng), f.lastName.Rand(rng), f.suffixMale.Rand(rng)) 64 }, 0.005, 65 ) 66 67 f.firstNameFemale = firstNameFemale() 68 f.firstNameMale = firstNameMale() 69 f.lastName = lastName() 70 f.prefixFemale = makeWeightedEntries( 71 `Mrs.`, 0.5, 72 `Ms.`, 0.1, 73 `Miss`, 0.1, 74 `Dr.`, 0.3, 75 ) 76 f.prefixMale = makeWeightedEntries( 77 `Mr.`, 0.7, 78 `Dr.`, 0.3, 79 ) 80 f.suffixFemale = makeWeightedEntries( 81 `MD`, 0.5, 82 `DDS`, 0.3, 83 `PhD`, 0.1, 84 `DVM`, 0.2, 85 ) 86 f.suffixMale = makeWeightedEntries( 87 `Jr.`, 0.2, 88 `II`, 0.05, 89 `III`, 0.03, 90 `IV`, 0.015, 91 `V`, 0.005, 92 `MD`, 0.3, 93 `DDS`, 0.2, 94 `PhD`, 0.1, 95 `DVM`, 0.1, 96 ) 97 return f 98 }