func DuplicateRequest(request *http.Request, r *rand.Rand) (request1 *http.Request, request2 *http.Request) { b1 := new(bytes.Buffer) b2 := new(bytes.Buffer) w := io.MultiWriter(b1, b2) io.Copy(w, request.Body) defer request.Body.Close() if r.Int31n(100) <= int32(*percent) { request1 = &http.Request{ Method: request.Method, URL: request.URL, Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1, Header: request.Header, Body: nopCloser{b1}, Host: request.Host, ContentLength: request.ContentLength, } } else { request1 = nil } request2 = &http.Request{ Method: request.Method, URL: request.URL, Proto: "HTTP/1.1", ProtoMajor: 1, ProtoMinor: 1, Header: request.Header, Body: nopCloser{b2}, Host: request.Host, ContentLength: request.ContentLength, } return }
func keyFromRand(r *rand.Rand) string { var key string for i := 0; i != 6; i++ { key += string(keyChars[r.Int31n(int32(len(keyChars)))]) } return key }
// MakePriority generates a random priority value, biased by the // specified userPriority. If userPriority=100, the resulting // priority is 100x more likely to be probabilistically greater // than a similar invocation with userPriority=1. func MakePriority(r *rand.Rand, userPriority int32) int32 { // A currently undocumented feature allows an explicit priority to // be set by specifying priority < 1. The explicit priority is // simply -userPriority in this case. This is hacky, but currently // used for unittesting. Perhaps this should be documented and allowed. if userPriority < 0 { return -userPriority } if userPriority == 0 { userPriority = 1 } // The idea here is to bias selection of a random priority from the // range [1, 2^31-1) such that if userPriority=100, it's 100x more // likely to be a higher int32 than if userPriority=1. The formula // below chooses random values according to the following table: // userPriority | range // 1 | all positive int32s // 10 | top 9/10ths of positive int32s // 100 | top 99/100ths of positive int32s // 1000 | top 999/1000ths of positive int32s // ...etc if r != nil { return math.MaxInt32 - r.Int31n(math.MaxInt32/userPriority) } return math.MaxInt32 - rand.Int31n(math.MaxInt32/userPriority) }
func growTree(chunk *chunk.Chunk, r *rand.Rand, x, y, z int32) { if x&0xF == x && z&0xF == z { return } if x&0xF < 2 || x&0xF > 13 || z&0xF < 2 || z&0xF > 13 { return } chunk.SetBlock(x, y-1, z, block.Dirt) height := r.Int31n(5) + 3 for Y := y; Y < y+height; Y++ { chunk.SetBlock(x, Y, z, block.Log) } for X := x - 2; X <= x+2; X++ { for Z := z - 2; Z <= z+2; Z++ { for Y := y + height - 2; Y <= y+height+2; Y++ { if chunk.GetBlock(X, Y, Z) == block.Air { chunk.SetBlock(X, Y, Z, block.Leaves) } } } } }
// this function inspired by http://devpy.wordpress.com/2013/10/24/create-random-string-in-golang/ func rand_str(str_size int, randSource *rand.Rand) string { alphanum := "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" var bytes = make([]byte, str_size) for i := 0; i < str_size; i++ { bytes[i] = alphanum[randSource.Int31n(int32(len(alphanum)))] } return string(bytes) }
func shuffle(l []*Person, r *rand.Rand) { // First sort them so that they are in a deterministic order so that the random assignment can be re-encountered if needed sort.Sort(People(l)) for i := int32(len(l) - 1); i > 0; i-- { j := r.Int31n(i + 1) l[i], l[j] = l[j], l[i] } }
func generateA(r *rand.Rand) *A { return &A{ Name: randString(r, 16), BirthDay: r.Int63(), Phone: randString(r, 10), Siblings: r.Int31n(5), Spouse: r.Intn(2) == 1, Money: r.Float64(), } }
func Permute(v []int64, r *rand.Rand) { n := int32(len(v)) for n > 0 { i := r.Int31n(n) aux := v[n-1] v[n-1] = v[i] v[i] = aux n-- } }
func GenString(template string, randSource *rand.Rand) string { var buf bytes.Buffer alpha := "abcdefghijklmnopqrstuvwxyz" nums := "0123456789" for i := 0; i < len(template); i++ { if template[i] == '#' { buf.WriteByte(nums[randSource.Int31n(int32(len(nums)))]) } else if template[i] == '@' { buf.WriteByte(alpha[randSource.Int31n(int32(len(alpha)))]) } else { buf.WriteByte(template[i]) } } return buf.String() }
func createword(random *rand.Rand) string { x := getword("") s := "" for { x = x.next[random.Int31n(int32(len(x.next)))] if x.str == "" { return s } s += x.str + " " } return "" }
func (b bytesize) Generate(rand *rand.Rand, size int) reflect.Value { suffix := "" number := 1 + rand.Int31n(int32(size)) chance := rand.Float32() if chance < 0.1 { suffix = "k" } // Can't test `kb`-style suffixes in darwin… return reflect.ValueOf(bytesize(fmt.Sprintf("%d%s", number, suffix))) }
func Numerify(pattern string, numerifyRune rune, rand *rand.Rand) string { result := []byte{} for _, char := range pattern { if char == numerifyRune { result = strconv.AppendInt(result, int64(rand.Int31n(10)), 10) } else { result = append(result, byte(char)) } } return string(result) }
func randomPoint(r *rand.Rand) *pb.Point { lat := (r.Int31n(180) - 90) * 1e7 long := (r.Int31n(360) - 180) * 1e7 return &pb.Point{lat, long} }
// rndValueType returns random value of type xValueType func rndValueType(rnd *rand.Rand) xValueType { return xValueType(rnd.Int31n(int32(xRnd + 1))) }
// Shuffle shuffles the cards in the deck using the // Knuth shuffle algorithm. func (d Deck) Shuffle(rng *rand.Rand) { for a := 0; a < len(d); a++ { b := rng.Int31n(int32(a) + 1) d[a], d[b] = d[b], d[a] } }
// rndZValueType returns random value of type zValueType func rndZValueType(rnd *rand.Rand) zValueType { return zValueType(rnd.Int31n(int32(zZero + 1))) }
func (s seek) Generate(r *rand.Rand, _ int) reflect.Value { s.offset = int64(r.Int31n(seekBytes)) return reflect.ValueOf(s) }