func NewMockDirectoryWrapper(random *rand.Rand, delegate store.Directory) *MockDirectoryWrapper { ans := &MockDirectoryWrapper{ noDeleteOpenFile: true, preventDoubleWrite: true, trackDiskUsage: false, wrapLockFactory: true, openFilesForWrite: make(map[string]bool), openLocks: make(map[string]bool), openLocksLock: &sync.Mutex{}, throttling: THROTTLING_SOMETIMES, inputCloneCount: 0, openFileHandles: make(map[io.Closer]error), failOnCreateOutput: true, failOnOpenInput: true, assertNoUnreferencedFilesOnClose: true, } ans.BaseDirectoryWrapperImpl = NewBaseDirectoryWrapper(delegate) ans.Locker = &sync.Mutex{} // must make a private random since our methods are called from different // methods; else test failures may not be reproducible from the original // seed ans.randomState = rand.New(rand.NewSource(random.Int63())) ans.throttledOutput = NewThrottledIndexOutput( MBitsToBytes(40+ans.randomState.Intn(10)), 5+ans.randomState.Int63n(5), nil) // force wrapping of LockFactory ans.myLockFactory = newMockLockFactoryWrapper(ans, delegate.LockFactory()) ans.init() return ans }
func rnds16(rng *rand.Rand, n int) string { a := make([]string, n) for i := range a { a[i] = fmt.Sprintf("%016x", rng.Int63()) } return strings.Join(a, "") }
func NewMockRandomMergePolicy(r *rand.Rand) *MockRandomMergePolicy { // fork a private random, since we are called unpredicatably from threads: res := &MockRandomMergePolicy{ random: rand.New(rand.NewSource(r.Int63())), } res.MergePolicyImpl = NewDefaultMergePolicyImpl(res) return res }
func readRand(r *rand.Rand, p []byte) { for i := 0; i < len(p); i += 7 { val := r.Int63() for j := 0; i+j < len(p) && j < 7; j++ { p[i+j] = byte(val) val >>= 8 } } }
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 (ptr *ParticleTracingRenderer) Render( numRenderJobs int, rng *rand.Rand, scene *Scene, outputDir, outputExt string) { var combinedLightConfig SampleConfig for _, light := range scene.Lights { lightConfig := light.GetSampleConfig() combinedLightConfig.CombineWith(&lightConfig) } var totalSampleCount int sensors := scene.Aggregate.GetSensors() for _, sensor := range sensors { extent := sensor.GetExtent() totalSampleCount += extent.GetSampleCount() } samplesPerJob := (totalSampleCount + numRenderJobs - 1) / numRenderJobs totalSampleCount = samplesPerJob * numRenderJobs channelSize := minInt(totalSampleCount, 1024) recordsCh := make(chan []TracerRecord, channelSize) for i := 0; i < numRenderJobs; i++ { workerRng := rand.New(rand.NewSource(rng.Int63())) go ptr.processSamples( workerRng, scene, sensors, combinedLightConfig, samplesPerJob, recordsCh) } progressInterval := (totalSampleCount + 99) / 100 for i := 0; i < totalSampleCount; i++ { records := <-recordsCh for j := 0; j < len(records); j++ { records[j].Accumulate() } for _, sensor := range sensors { sensor.RecordAccumulatedLightContributions() } if (i+1)%progressInterval == 0 || i+1 == totalSampleCount { fmt.Printf("Processed %d/%d sample(s)\n", i+1, totalSampleCount) } if ((i + 1) == totalSampleCount) || (ptr.emitInterval > 0 && (i+1)%ptr.emitInterval == 0) { for _, sensor := range sensors { sensor.EmitSignal(outputDir, outputExt) } } } }
func generateRandomRefValue() string { var r *rand.Rand sval := "" for len(sval) < 16 { r = rand.New(rand.NewSource(int64(time.Now().UnixNano()))) sval = fmt.Sprintf("%x", r.Int63()) } return sval }
func NonZeroRand64(rnd *rand.Rand) int64 { for { r := rnd.Int63() if r == 0 { continue } if rnd.Intn(1) != 0 { return -r } return r } }
// Creates a new MockAnalyzer. func NewMockAnalyzer(r *rand.Rand, runAutomaton *auto.CharacterRunAutomaton, lowerCase bool, filter *auto.CharacterRunAutomaton) *MockAnalyzer { return &MockAnalyzer{ AnalyzerImpl: ca.NewAnalyzerWithStrategy(ca.PER_FIELD_REUSE_STRATEGY), // TODO: this should be solved in a different way; Random should not be shared (!) random: rand.New(rand.NewSource(r.Int63())), runAutomaton: runAutomaton, lowerCase: lowerCase, filter: filter, previousMappings: make(map[string]int), enableChecks: true, maxTokenLength: DEFAULT_MAX_TOKEN_LENGTH, } }
// Print random int63 number from "mt19937_64". // Reset random number generator if "i" is 13. func PrintInt63(i int, mt *rand.Rand, ch chan bool) { t := rand.Int() % 1000 time.Sleep(time.Duration(t)) if 13 == i { mt.Seed(12345) fmt.Printf("13 : (reset) \n") } else { fmt.Printf("%2d : %d \n", i, mt.Int63()) } ch <- true }
func ExampleSource() { var r *rand.Rand // Instead of this r = rand.New(rand.NewSource(time.Now().UnixNano())) // Use this... r = rand.New(Source{}) // and then call this... r.Int63() }
// Generate a random string of size n. func randomString(n int, rng *rand.Rand) string { b := make([]byte, n) for i, cache, remain := n-1, rng.Int63(), letterIdxMax; i >= 0; { if remain == 0 { cache, remain = rng.Int63(), letterIdxMax } if idx := int(cache & letterIdxMask); idx < len(letterBytes) { b[i] = letterBytes[idx] i-- } cache >>= letterIdxBits remain-- } return string(b) }
func (c Publishing) Generate(r *rand.Rand, _ int) reflect.Value { var ok bool var t reflect.Value p := Publishing{} //p.DeliveryMode = uint8(r.Intn(3)) //p.Priority = uint8(r.Intn(8)) if r.Intn(2) > 0 { p.ContentType = "application/octet-stream" } if r.Intn(2) > 0 { p.ContentEncoding = "gzip" } if r.Intn(2) > 0 { p.CorrelationId = fmt.Sprintf("%d", r.Int()) } if r.Intn(2) > 0 { p.ReplyTo = fmt.Sprintf("%d", r.Int()) } if r.Intn(2) > 0 { p.MessageId = fmt.Sprintf("%d", r.Int()) } if r.Intn(2) > 0 { p.Type = fmt.Sprintf("%d", r.Int()) } if r.Intn(2) > 0 { p.AppId = fmt.Sprintf("%d", r.Int()) } if r.Intn(2) > 0 { p.Timestamp = time.Unix(r.Int63(), r.Int63()) } if t, ok = quick.Value(reflect.TypeOf(p.Body), r); ok { p.Body = t.Bytes() } return reflect.ValueOf(p) }
func GenerateD(d int64, random *rand.Rand) []float64 { var i, j, l int64 data := make([]float64, d) for i = 0; i < d; { l = random.Int63() for j = 0; j < 32 && i < d; j++ { if (l & 1) == 1 { data[i] = 1 } else { data[i] = -1 } l = l >> 1 i++ } } return data }
// RandDatum generates a random Datum of the given type. // If null is true, the datum can be DNull. func RandDatum(rng *rand.Rand, typ ColumnType_Kind, null bool) parser.Datum { if null && rng.Intn(10) == 0 { return parser.DNull } switch typ { case ColumnType_BOOL: return parser.MakeDBool(rng.Intn(2) == 1) case ColumnType_INT: return parser.NewDInt(parser.DInt(rng.Int63())) case ColumnType_FLOAT: return parser.NewDFloat(parser.DFloat(rng.NormFloat64())) case ColumnType_DECIMAL: d := &parser.DDecimal{} d.Dec.SetScale(inf.Scale(rng.Intn(40) - 20)) d.Dec.SetUnscaled(rng.Int63()) return d case ColumnType_DATE: return parser.NewDDate(parser.DDate(rng.Intn(10000))) case ColumnType_TIMESTAMP: return &parser.DTimestamp{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))} case ColumnType_INTERVAL: return &parser.DInterval{Duration: duration.Duration{Months: rng.Int63n(1000), Days: rng.Int63n(1000), Nanos: rng.Int63n(1000000), }} case ColumnType_STRING: // Generate a random ASCII string. p := make([]byte, rng.Intn(10)) for i := range p { p[i] = byte(1 + rng.Intn(127)) } return parser.NewDString(string(p)) case ColumnType_BYTES: p := make([]byte, rng.Intn(10)) _, _ = rng.Read(p) return parser.NewDBytes(parser.DBytes(p)) case ColumnType_TIMESTAMPTZ: return &parser.DTimestampTZ{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))} case ColumnType_INT_ARRAY: // TODO(cuongdo): we don't support for persistence of arrays yet return parser.DNull default: panic(fmt.Sprintf("invalid type %s", typ)) } }
func createPosts(chain *Chain, count int, rand *rand.Rand) []testPost { posts := make([]testPost, count) for i := 0; i < count; i++ { text := chain.Generate(20, rand) posts[i] = testPost{id: (uint64)(rand.Int63()), words: splitToWords(text)} } return posts }
func TestBloomFilter(r *rand.Rand, size, numAdd, numSample, numHash uint) { falsePositive := 0 falseNegative := 0 // should not occur! truePositive := 0 trueNegative := 0 iterations := 10000 for iteration := 0; iteration < iterations; iteration++ { hashes := make([]uint64, numHash, numHash) for i := uint(0); i < numHash; i++ { hashes[i] = uint64(r.Uint32()) } bloom := New(size, hashes) realset := make(map[uint64]bool) generate := func() uint64 { return uint64(r.Int63()) } for i := uint(0); i < numAdd; i++ { generated := generate() bloom.Add(generated) realset[generated] = true } for i := uint(0); i < numSample; i++ { generated := generate() bloomResult := bloom.MaybeContains(generated) _, realResult := realset[generated] if bloomResult && realResult { truePositive++ } else if !bloomResult && !realResult { trueNegative++ } else if bloomResult && !realResult { falsePositive++ } else if !bloomResult && realResult { falseNegative++ } } } fmt.Printf(" %12d %12d %12d | %02.4f\n", size, numAdd, numHash, float32(falsePositive)/float32(numSample)*100/float32(iterations), ) }
// Make a network of nodes with random topology func makeRandomModel(params *TestParams, r *rand.Rand, t *testing.T) *Model { m := Model{ t: t, r: r, quorum: params.nodeCount/2 + 1, nodes: make([]TestNode, params.nodeCount), readyLinks: []*Link{}, nextID: params.nodeCount + 1, } for i := range m.nodes { m.nodes[i].Node = NewNode(router.PeerName(i/2+1), router.PeerUID(r.Int63()), m.quorum) m.nodes[i].Propose() } for i := 1; i < len(m.nodes); i++ { // was node i connected to the other nodes yet? connected := false for j := 0; j < i; j++ { if r.Float32() < params.connectedProb { connected = true m.addLink(&m.nodes[i], &m.nodes[j]) } } if !connected { // node i must be connected into the graph // somewhere. So if we didn't connect it // already, this is a last resort. m.addLink(&m.nodes[i], &m.nodes[r.Intn(i)]) } } return &m }
func (twptr *TwoWayPathTracingRenderer) processSensor( numRenderJobs int, rng *rand.Rand, scene *Scene, sensors []Sensor, sensor Sensor, lightConfig SampleConfig, outputDir, outputExt string) { blockCh := make(chan twptBlock, numRenderJobs) defer close(blockCh) processedBlockCh := make(chan processedTwptBlock, numRenderJobs) xBlockSize := 32 yBlockSize := 32 sBlockSize := 32 sensorExtent := sensor.GetExtent() var blockOrder SensorExtentBlockOrder if twptr.emitInterval > 0 { blockOrder = SENSOR_EXTENT_SXY } else { blockOrder = SENSOR_EXTENT_XYS } blocks := sensorExtent.Split( blockOrder, xBlockSize, yBlockSize, sBlockSize) for i := 0; i < numRenderJobs; i++ { workerRng := rand.New(rand.NewSource(rng.Int63())) go twptr.processBlocks( workerRng, scene, sensors, sensor, sBlockSize, lightConfig, blockCh, processedBlockCh) } numBlocks := len(blocks) recordBlockSamples := func(processedBlock processedTwptBlock) { block := processedBlock.block fmt.Printf("Finished block %d/%d\n", block.blockNumber+1, numBlocks) sensorRecords := processedBlock.sensorRecords lightRecords := processedBlock.lightRecords for j := 0; j < len(sensorRecords); j++ { sensorRecords[j].Accumulate() for k := 0; k < len(lightRecords[j]); k++ { lightRecords[j][k].Accumulate() } for _, sensor := range sensors { sensor.RecordAccumulatedLightContributions() } } } processed := 0 maybeEmit := func() { if twptr.emitInterval > 0 && processed%twptr.emitInterval == 0 { for _, sensor := range sensors { sensor.EmitSignal(outputDir, outputExt) } } } for i := 0; i < len(blocks); { select { case processedBlock := <-processedBlockCh: recordBlockSamples(processedBlock) processed++ maybeEmit() default: fmt.Printf("Queueing block %d/%d\n", i+1, numBlocks) blockCh <- twptBlock{i, blocks[i]} i++ } } for processed < len(blocks) { processedBlock := <-processedBlockCh recordBlockSamples(processedBlock) processed++ maybeEmit() } }
func getKey(r *rand.Rand) []byte { return []byte(fmt.Sprintf("%d", r.Int63())) }
// Avoid math.rand.Rand.Float32() since it's buggy; see // https://code.google.com/p/go/issues/detail?id=6721 . func randFloat32(rng *rand.Rand) float32 { x := rng.Int63() // Use the top 24 bits of x for f's significand. f := float32(x>>39) / float32(1<<24) return f }
// randInt64 returns a random integer taking half the range of an int64. func randInt64(rand *rand.Rand) int64 { return rand.Int63() - 1<<62 }
func newAlcoholicMergePolicy(r *rand.Rand /*, tz TimeZone*/) *ti.AlcoholicMergePolicy { return ti.NewAlcoholicMergePolicy(rand.New(rand.NewSource(r.Int63()))) }
func NewAssertingIndexSearcher(random *rand.Rand, r index.IndexReader) *AssertingIndexSearcher { return &AssertingIndexSearcher{ search.NewIndexSearcher(r), rand.New(rand.NewSource(random.Int63())), } }
func (k *KeyData) setCas(r *rand.Rand) *KeyData { k.Cas = r.Int63() return k }
func NewAssertingIndexSearcherFromContext(random *rand.Rand, ctx index.IndexReaderContext) *AssertingIndexSearcher { return &AssertingIndexSearcher{ search.NewIndexSearcherFromContext(ctx), rand.New(rand.NewSource(random.Int63())), } }
func (k *IfaceKey) random(r *rand.Rand) { k.i = fInter(r.Int63()) }
func (k *EfaceKey) random(r *rand.Rand) { k.i = uint64(r.Int63()) }
// Fill fills all exported fields of a struct with random data func Fill(val reflect.Value, rand *rand.Rand) { if !val.CanSet() { return } switch { case val.Type().Implements(fillerType): t := val.Type() numIndirects := 0 for ; t.Kind() == reflect.Ptr; numIndirects++ { t = t.Elem() } v := reflect.New(t).Elem() for ; numIndirects > 0; numIndirects-- { v = v.Addr() } v.Interface().(RandFiller).RandFill(rand) val.Set(v) case val.Type() == timeType: val.Set(reflect.ValueOf(time.Date( rand.Intn(50)+1990, time.Month(rand.Intn(12)), rand.Intn(28), rand.Intn(24), rand.Intn(60), rand.Intn(60), rand.Intn(1e9), time.UTC), )) default: switch val.Type().Kind() { case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64: val.SetUint(uint64(rand.Int63())) case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64: val.SetInt(rand.Int63()) case reflect.String: val.SetString("string") case reflect.Bool: val.SetBool(rand.Int()%2 == 0) case reflect.Float32, reflect.Float64: val.SetFloat(rand.Float64()) case reflect.Slice: length := rand.Intn(5) + 3 slice := reflect.MakeSlice(val.Type(), length, length) for i := 0; i < length; i++ { v := reflect.New(val.Type().Elem()) Fill(v.Elem(), rand) slice.Index(i).Set(v.Elem()) } val.Set(slice) case reflect.Ptr: elem := reflect.New(val.Type().Elem()) Fill(elem.Elem(), rand) val.Set(elem) case reflect.Struct: numFields := val.NumField() for i := 0; i < numFields; i++ { Fill(val.Field(i), rand) } default: fmt.Println("Unsupported field: ", val.Type()) } } }
// RandDatum generates a random Datum of the given type. // If null is true, the datum can be DNull. func RandDatum(rng *rand.Rand, typ ColumnType, null bool) parser.Datum { if null && rng.Intn(10) == 0 { return parser.DNull } switch typ.Kind { case ColumnType_BOOL: return parser.MakeDBool(rng.Intn(2) == 1) case ColumnType_INT: return parser.NewDInt(parser.DInt(rng.Int63())) case ColumnType_FLOAT: return parser.NewDFloat(parser.DFloat(rng.NormFloat64())) case ColumnType_DECIMAL: d := &parser.DDecimal{} d.Dec.SetScale(inf.Scale(rng.Intn(40) - 20)) d.Dec.SetUnscaled(rng.Int63()) return d case ColumnType_DATE: return parser.NewDDate(parser.DDate(rng.Intn(10000))) case ColumnType_TIMESTAMP: return &parser.DTimestamp{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))} case ColumnType_INTERVAL: return &parser.DInterval{Duration: duration.Duration{Months: rng.Int63n(1000), Days: rng.Int63n(1000), Nanos: rng.Int63n(1000000), }} case ColumnType_STRING: // Generate a random ASCII string. p := make([]byte, rng.Intn(10)) for i := range p { p[i] = byte(1 + rng.Intn(127)) } return parser.NewDString(string(p)) case ColumnType_BYTES: p := make([]byte, rng.Intn(10)) _, _ = rng.Read(p) return parser.NewDBytes(parser.DBytes(p)) case ColumnType_TIMESTAMPTZ: return &parser.DTimestampTZ{Time: time.Unix(rng.Int63n(1000000), rng.Int63n(1000000))} case ColumnType_COLLATEDSTRING: if typ.Locale == nil { panic("locale is required for COLLATEDSTRING") } // Generate a random Unicode string. var buf bytes.Buffer n := rng.Intn(10) for i := 0; i < n; i++ { var r rune for { r = rune(rng.Intn(unicode.MaxRune + 1)) if !unicode.Is(unicode.C, r) { break } } buf.WriteRune(r) } return parser.NewDCollatedString(buf.String(), *typ.Locale, &parser.CollationEnvironment{}) case ColumnType_INT_ARRAY: // TODO(cuongdo): we don't support for persistence of arrays yet return parser.DNull default: panic(fmt.Sprintf("invalid type %s", typ.String())) } }