github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/workload/movr/rand.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 movr
    12  
    13  import (
    14  	"encoding/json"
    15  
    16  	"golang.org/x/exp/rand"
    17  )
    18  
    19  const numerals = `1234567890`
    20  
    21  var vehicleTypes = [...]string{`skateboard`, `bike`, `scooter`}
    22  var vehicleColors = [...]string{`red`, `yellow`, `blue`, `green`, `black`}
    23  var bikeBrands = [...]string{
    24  	`Merida`, `Fuji`, `Cervelo`, `Pinarello`, `Santa Cruz`, `Kona`, `Schwinn`}
    25  
    26  func randString(rng *rand.Rand, length int, alphabet string) string {
    27  	buf := make([]byte, length)
    28  	for i := range buf {
    29  		buf[i] = alphabet[rng.Intn(len(alphabet))]
    30  	}
    31  	return string(buf)
    32  }
    33  
    34  func randCreditCard(rng *rand.Rand) string {
    35  	return randString(rng, 10, numerals)
    36  }
    37  
    38  func randVehicleType(rng *rand.Rand) string {
    39  	return vehicleTypes[rng.Intn(len(vehicleTypes))]
    40  }
    41  
    42  func randVehicleStatus(rng *rand.Rand) string {
    43  	r := rng.Intn(100)
    44  	switch {
    45  	case r < 40:
    46  		return `available`
    47  	case r < 95:
    48  		return `in_use`
    49  	default:
    50  		return `lost`
    51  	}
    52  }
    53  
    54  func randLatLong(rng *rand.Rand) (float64, float64) {
    55  	lat, long := float64(-180+rng.Intn(360)), float64(-90+rng.Intn(180))
    56  	return lat, long
    57  }
    58  
    59  func randCity(rng *rand.Rand) string {
    60  	idx := rng.Int31n(int32(len(cities)))
    61  	return cities[idx].city
    62  }
    63  
    64  func randVehicleMetadata(rng *rand.Rand, vehicleType string) string {
    65  	m := map[string]string{
    66  		`color`: vehicleColors[rng.Intn(len(vehicleColors))],
    67  	}
    68  	switch vehicleType {
    69  	case `bike`:
    70  		m[`brand`] = bikeBrands[rng.Intn(len(bikeBrands))]
    71  	}
    72  	j, err := json.Marshal(m)
    73  	if err != nil {
    74  		panic(err)
    75  	}
    76  	return string(j)
    77  }