Matthias Andreas Benkard | 832a54e | 2019-01-29 09:27:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | Copyright 2015 The Kubernetes Authors. |
| 3 | |
| 4 | Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | you may not use this file except in compliance with the License. |
| 6 | You may obtain a copy of the License at |
| 7 | |
| 8 | http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | |
| 10 | Unless required by applicable law or agreed to in writing, software |
| 11 | distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | See the License for the specific language governing permissions and |
| 14 | limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | // Package rand provides utilities related to randomization. |
| 18 | package rand |
| 19 | |
| 20 | import ( |
| 21 | "math/rand" |
| 22 | "sync" |
| 23 | "time" |
| 24 | ) |
| 25 | |
| 26 | var rng = struct { |
| 27 | sync.Mutex |
| 28 | rand *rand.Rand |
| 29 | }{ |
| 30 | rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), |
| 31 | } |
| 32 | |
| 33 | // Intn generates an integer in range [0,max). |
| 34 | // By design this should panic if input is invalid, <= 0. |
| 35 | func Intn(max int) int { |
| 36 | rng.Lock() |
| 37 | defer rng.Unlock() |
| 38 | return rng.rand.Intn(max) |
| 39 | } |
| 40 | |
| 41 | // IntnRange generates an integer in range [min,max). |
| 42 | // By design this should panic if input is invalid, <= 0. |
| 43 | func IntnRange(min, max int) int { |
| 44 | rng.Lock() |
| 45 | defer rng.Unlock() |
| 46 | return rng.rand.Intn(max-min) + min |
| 47 | } |
| 48 | |
| 49 | // IntnRange generates an int64 integer in range [min,max). |
| 50 | // By design this should panic if input is invalid, <= 0. |
| 51 | func Int63nRange(min, max int64) int64 { |
| 52 | rng.Lock() |
| 53 | defer rng.Unlock() |
| 54 | return rng.rand.Int63n(max-min) + min |
| 55 | } |
| 56 | |
| 57 | // Seed seeds the rng with the provided seed. |
| 58 | func Seed(seed int64) { |
| 59 | rng.Lock() |
| 60 | defer rng.Unlock() |
| 61 | |
| 62 | rng.rand = rand.New(rand.NewSource(seed)) |
| 63 | } |
| 64 | |
| 65 | // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) |
| 66 | // from the default Source. |
| 67 | func Perm(n int) []int { |
| 68 | rng.Lock() |
| 69 | defer rng.Unlock() |
| 70 | return rng.rand.Perm(n) |
| 71 | } |
| 72 | |
| 73 | const ( |
| 74 | // We omit vowels from the set of available characters to reduce the chances |
| 75 | // of "bad words" being formed. |
| 76 | alphanums = "bcdfghjklmnpqrstvwxz2456789" |
| 77 | // No. of bits required to index into alphanums string. |
| 78 | alphanumsIdxBits = 5 |
| 79 | // Mask used to extract last alphanumsIdxBits of an int. |
| 80 | alphanumsIdxMask = 1<<alphanumsIdxBits - 1 |
| 81 | // No. of random letters we can extract from a single int63. |
| 82 | maxAlphanumsPerInt = 63 / alphanumsIdxBits |
| 83 | ) |
| 84 | |
| 85 | // String generates a random alphanumeric string, without vowels, which is n |
| 86 | // characters long. This will panic if n is less than zero. |
| 87 | // How the random string is created: |
| 88 | // - we generate random int63's |
| 89 | // - from each int63, we are extracting multiple random letters by bit-shifting and masking |
| 90 | // - if some index is out of range of alphanums we neglect it (unlikely to happen multiple times in a row) |
| 91 | func String(n int) string { |
| 92 | b := make([]byte, n) |
| 93 | rng.Lock() |
| 94 | defer rng.Unlock() |
| 95 | |
| 96 | randomInt63 := rng.rand.Int63() |
| 97 | remaining := maxAlphanumsPerInt |
| 98 | for i := 0; i < n; { |
| 99 | if remaining == 0 { |
| 100 | randomInt63, remaining = rng.rand.Int63(), maxAlphanumsPerInt |
| 101 | } |
| 102 | if idx := int(randomInt63 & alphanumsIdxMask); idx < len(alphanums) { |
| 103 | b[i] = alphanums[idx] |
| 104 | i++ |
| 105 | } |
| 106 | randomInt63 >>= alphanumsIdxBits |
| 107 | remaining-- |
| 108 | } |
| 109 | return string(b) |
| 110 | } |
| 111 | |
| 112 | // SafeEncodeString encodes s using the same characters as rand.String. This reduces the chances of bad words and |
| 113 | // ensures that strings generated from hash functions appear consistent throughout the API. |
| 114 | func SafeEncodeString(s string) string { |
| 115 | r := make([]byte, len(s)) |
| 116 | for i, b := range []rune(s) { |
| 117 | r[i] = alphanums[(int(b) % len(alphanums))] |
| 118 | } |
| 119 | return string(r) |
| 120 | } |