本文整理汇总了Golang中math/rand.Rand.Int方法的典型用法代码示例。如果您正苦于以下问题:Golang Rand.Int方法的具体用法?Golang Rand.Int怎么用?Golang Rand.Int使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类math/rand.Rand
的用法示例。
在下文中一共展示了Rand.Int方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: rndBytes
func rndBytes(rng *rand.Rand, n int) []byte {
r := make([]byte, n)
for i := range r {
r[i] = byte(rng.Int())
}
return r
}
示例2: main
func main() {
var (
nbLigne uint
result [][]int
r *rand.Rand
i uint
)
nbLigne = Initialisation()
result = make([][]int, nbLigne)
r = rand.New(rand.NewSource(time.Now().UnixNano()))
for i = 0; i < nbLigne; i++ {
result[i] = make([]int, 3)
result[i][0] = int(math.Mod(float64(r.Int()), 200)) - 100
result[i][1] = int(math.Mod(float64(r.Int()), 200)) - 100
result[i][2] = int(math.Mod(float64(r.Int()), 9)) + 1
}
ecrireFichier("fichierTest", nbLigne, result)
}
示例3: Tick
func (g *game) Tick(p *rand.Rand) (food bool, selfeat []int) {
// r := rand.New(rand.NewSource(p))
var tempTail points
for i, sn := range g.Snake {
tempTail = g.Snake[i].Body[len(sn.Body)-1]
for q, _ := range sn.Body[:len(sn.Body)-1] {
g.Snake[i].Body[len(sn.Body)-q-1] = g.Snake[i].Body[len(sn.Body)-q-2]
}
switch sn.Dir {
case "UP":
g.Snake[i].Body[0].Y--
case "DOWN":
g.Snake[i].Body[0].Y++
case "LEFT":
g.Snake[i].Body[0].X--
case "RIGHT":
g.Snake[i].Body[0].X++
}
food = g.EatFood(tempTail)
selfeat = g.EatSelf()
if len(g.Food) == 0 {
g.AddFood(p.Int()%30, p.Int()%30)
}
fmt.Println(sn)
}
return food, selfeat
}
示例4: genData
// Generate array of random data for transmission
func genData(n int, rgen rand.Rand) []int {
result := make([]int, n)
for i := 0; i < n; i++ {
result[i] = rgen.Int()
}
return result
}
示例5: Qsort
func Qsort(a Interface, prng *rand.Rand) Interface {
if a.Len() < 2 {
return a
}
left, right := 0, a.Len()-1
pivotIndex := prng.Int() % a.Len()
a.Swap(pivotIndex, right)
for i := 0; i < a.Len(); i++ {
if a.Less(i, right) {
a.Swap(i, left)
left++
}
}
a.Swap(left, right)
leftSide, rightSide := a.Partition(left)
Qsort(leftSide, prng)
Qsort(rightSide, prng)
return a
}
示例6: TestPriorityQueue_order_Time
func TestPriorityQueue_order_Time(t *testing.T) {
pq := NewPriorityQueue(Less(func(x, y interface{}) bool {
return x.(time.Time).Before(y.(time.Time))
}), 10)
//Populate the priority queue with random times
var src rand.Source = rand.NewSource(0)
var r *rand.Rand = rand.New(src)
for i := 0; i < 10; i++ {
assert.True(
t,
pq.Length() == i,
"pq.Length() = %d; want %d", pq.Length(), i,
)
pq.Insert(time.Now().Add(time.Hour * time.Duration(r.Int())))
}
var prev time.Time = pq.PopTop().(time.Time)
var next time.Time
for pq.Length() > 0 {
next = pq.PopTop().(time.Time)
assert.True(
t,
prev.Before(next),
"%s sorted before %s; want %s sorted after %s", prev, next, prev, next,
)
}
}
示例7: Emitter
func (self *Scene) Emitter(r *rand.Rand) *memit.Emitter {
if len(self.emitters) == 0 {
return nil
}
i := r.Int() % len(self.emitters)
return &self.emitters[i]
}
示例8: Shuffle
//Shuffle cards in the deck
func (deck *Deck) Shuffle(r *rand.Rand) {
data := *deck
l := len(data)
for i := 0; i < l; i++ {
j := r.Int() % (i + 1)
data[i], data[j] = data[j], data[i]
}
}
示例9: randomString
func randomString(prng *rand.Rand, length int) string {
var b bytes.Buffer
for i := 0; i < length; i += 1 {
b.WriteByte(byte(prng.Int() % 256))
}
return b.String()
}
示例10: Generate
func (r randomSplitter) Generate(rand *rand.Rand, size int) reflect.Value {
s := rand.Int() % 8
b := make([]byte, s)
for i, _ := range b {
b[i] = split[rand.Int()%3]
}
return reflect.ValueOf(randomSplitter(b))
}
示例11: unifPoint
// Returns a point chosen such that each pixel on the image has an
// equally likely chance of being painted; we do this by sampling
// points up to one radius away from the edges of the image.
func unifPoint(img image.Image, r int, rnd *rand.Rand) (int, int) {
bounds := img.Bounds()
dx := (bounds.Max.X - bounds.Min.X)
dy := (bounds.Max.Y - bounds.Min.Y)
x := bounds.Min.X + rnd.Int()%dx
y := bounds.Min.Y + rnd.Int()%dy
return x, y
}
示例12: randomPset
// randomPset returns a parallel set of random size and elements.
func randomPset(prng *rand.Rand, maxSize int) *pset {
set := makePset()
size := int(prng.Int()) % maxSize
for i := 0; i < size; i++ {
// TODO(adonovan): benchmark how performance varies
// with this sparsity parameter.
n := int(prng.Int()) % 10000
set.add(n)
}
return set
}
示例13: TestPriorityQueue_order_int
func TestPriorityQueue_order_int(t *testing.T) {
pq := NewPriorityQueue(Less(func(x, y interface{}) bool {
return x.(int) < y.(int)
}), 0)
//Populate the priority queue with random ints
var src rand.Source = rand.NewSource(0)
var r *rand.Rand = rand.New(src)
items := make(map[int]bool)
for i := 0; i < 10; i++ {
assert.True(
t,
pq.Length() == i,
"pq.Length() = %d; want %d", pq.Length(), i,
)
n := r.Int()
pq.Insert(n)
items[n] = true
}
// test Items
sortedItems := make([]int, len(items))
ctr := 0
for n, _ := range items {
sortedItems[ctr] = n
ctr++
}
sort.Ints(sortedItems)
recItems := pq.Items()
recInts := make([]int, len(recItems))
for i, n := range recItems {
recInts[i] = n.(int)
}
sort.Ints(recInts)
for i, n := range recInts {
assert.True(
t,
sortedItems[i] == n,
"put and recovered items %d, &d not equal", sortedItems[i], n,
)
}
// test order
var prev int = pq.PopTop().(int)
var next int
for pq.Length() > 0 {
next = pq.PopTop().(int)
assert.True(
t,
prev <= next,
"%d sorted before %d; want %d sorted after %d", prev, next, prev, next,
)
}
}
示例14: redistributeRDY
func (r *Consumer) redistributeRDY(rng *rand.Rand) {
if r.inBackoffBlock() {
return
}
numConns := int32(len(r.conns()))
maxInFlight := r.getMaxInFlight()
if numConns > maxInFlight {
r.log(LogLevelDebug, "redistributing RDY state (%d conns > %d max_in_flight)",
numConns, maxInFlight)
atomic.StoreInt32(&r.needRDYRedistributed, 1)
}
if r.inBackoff() && numConns > 1 {
r.log(LogLevelDebug, "redistributing RDY state (in backoff and %d conns > 1)", numConns)
atomic.StoreInt32(&r.needRDYRedistributed, 1)
}
if !atomic.CompareAndSwapInt32(&r.needRDYRedistributed, 1, 0) {
return
}
conns := r.conns()
possibleConns := make([]*Conn, 0, len(conns))
for _, c := range conns {
lastMsgDuration := time.Now().Sub(c.LastMessageTime())
rdyCount := c.RDY()
r.log(LogLevelDebug, "(%s) rdy: %d (last message received %s)",
c.String(), rdyCount, lastMsgDuration)
if rdyCount > 0 && lastMsgDuration > r.config.LowRdyIdleTimeout {
r.log(LogLevelDebug, "(%s) idle connection, giving up RDY", c.String())
r.updateRDY(c, 0)
}
possibleConns = append(possibleConns, c)
}
availableMaxInFlight := int64(maxInFlight) - atomic.LoadInt64(&r.totalRdyCount)
if r.inBackoff() {
availableMaxInFlight = 1 - atomic.LoadInt64(&r.totalRdyCount)
}
for len(possibleConns) > 0 && availableMaxInFlight > 0 {
availableMaxInFlight--
i := rng.Int() % len(possibleConns)
c := possibleConns[i]
// delete
possibleConns = append(possibleConns[:i], possibleConns[i+1:]...)
r.log(LogLevelDebug, "(%s) redistributing RDY", c.String())
r.updateRDY(c, 1)
}
}
示例15: Generate
func (_ VbmapParams) Generate(rand *rand.Rand, size int) reflect.Value {
nodes := rand.Int()%100 + 1
replicas := rand.Int() % 4
params = VbmapParams{
Tags: trivialTags(nodes),
NumNodes: nodes,
NumSlaves: 10,
NumVBuckets: 1024,
NumReplicas: replicas,
}
normalizeParams(¶ms)
return reflect.ValueOf(params)
}