github.com/timstclair/heapster@v0.20.0-alpha1/Godeps/_workspace/src/k8s.io/kubernetes/pkg/util/rand/rand.go (about) 1 /* 2 Copyright 2015 The Kubernetes Authors All rights reserved. 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 letters = []rune("abcdefghijklmnopqrstuvwxyz0123456789") 27 var numLetters = len(letters) 28 var rng = struct { 29 sync.Mutex 30 rand *rand.Rand 31 }{ 32 rand: rand.New(rand.NewSource(time.Now().UTC().UnixNano())), 33 } 34 35 // String generates a random alphanumeric string n characters long. This will 36 // panic if n is less than zero. 37 func String(n int) string { 38 if n < 0 { 39 panic("out-of-bounds value") 40 } 41 b := make([]rune, n) 42 rng.Lock() 43 defer rng.Unlock() 44 for i := range b { 45 b[i] = letters[rng.rand.Intn(numLetters)] 46 } 47 return string(b) 48 } 49 50 // Seed seeds the rng with the provided seed. 51 func Seed(seed int64) { 52 rng.Lock() 53 defer rng.Unlock() 54 55 rng.rand = rand.New(rand.NewSource(seed)) 56 } 57 58 // Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n) 59 // from the default Source. 60 func Perm(n int) []int { 61 rng.Lock() 62 defer rng.Unlock() 63 return rng.rand.Perm(n) 64 }