Line data Source code
1 : // Copyright 2018 The Cockroach Authors. 2 : // 3 : // Licensed under the Apache License, Version 2.0 (the "License"); 4 : // you may not use this file except in compliance with the License. 5 : // You may obtain a copy of the License at 6 : // 7 : // http://www.apache.org/licenses/LICENSE-2.0 8 : // 9 : // Unless required by applicable law or agreed to in writing, software 10 : // distributed under the License is distributed on an "AS IS" BASIS, 11 : // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or 12 : // implied. See the License for the specific language governing 13 : // permissions and limitations under the License. See the AUTHORS file 14 : // for names of contributors. 15 : 16 : package randvar 17 : 18 : import ( 19 : "math/rand/v2" 20 : "sync" 21 : ) 22 : 23 : // SkewedLatest is a random number generator that generates numbers in 24 : // the range [min, max], but skews it towards max using a zipfian 25 : // distribution. 26 : type SkewedLatest struct { 27 : mu struct { 28 : sync.RWMutex 29 : max uint64 30 : zipf *Zipf 31 : } 32 : } 33 : 34 : // NewDefaultSkewedLatest constructs a new SkewedLatest generator with the 35 : // default parameters. 36 0 : func NewDefaultSkewedLatest() (*SkewedLatest, error) { 37 0 : return NewSkewedLatest(1, defaultMax, defaultTheta) 38 0 : } 39 : 40 : // NewSkewedLatest constructs a new SkewedLatest generator with the given 41 : // parameters. It returns an error if the parameters are outside the accepted 42 : // range. 43 2 : func NewSkewedLatest(min, max uint64, theta float64) (*SkewedLatest, error) { 44 2 : z := &SkewedLatest{} 45 2 : z.mu.max = max 46 2 : zipf, err := NewZipf(0, max-min, theta) 47 2 : if err != nil { 48 0 : return nil, err 49 0 : } 50 2 : z.mu.zipf = zipf 51 2 : return z, nil 52 : } 53 : 54 : // IncMax increments max. 55 1 : func (z *SkewedLatest) IncMax(delta int) { 56 1 : z.mu.Lock() 57 1 : z.mu.zipf.IncMax(delta) 58 1 : z.mu.max += uint64(delta) 59 1 : z.mu.Unlock() 60 1 : } 61 : 62 : // Max returns max. 63 1 : func (z *SkewedLatest) Max() uint64 { 64 1 : z.mu.Lock() 65 1 : defer z.mu.Unlock() 66 1 : return z.mu.zipf.Max() 67 1 : } 68 : 69 : // Uint64 returns a random Uint64 between min and max, where keys near max are 70 : // most likely to be drawn. 71 1 : func (z *SkewedLatest) Uint64(rng *rand.Rand) uint64 { 72 1 : z.mu.RLock() 73 1 : result := z.mu.max - z.mu.zipf.Uint64(rng) 74 1 : z.mu.RUnlock() 75 1 : return result 76 1 : }