本文整理汇总了Golang中math/rand.Uint32函数的典型用法代码示例。如果您正苦于以下问题:Golang Uint32函数的具体用法?Golang Uint32怎么用?Golang Uint32使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Uint32函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: rand32
func rand32() uint32 {
var result uint32 = 0
result = rand.Uint32()
result <<= 16
result |= rand.Uint32()
return result
}
示例2: TestFtoaRandom
func TestFtoaRandom(t *testing.T) {
N := int(1e4)
if testing.Short() {
N = 100
}
t.Logf("testing %d random numbers with fast and slow FormatFloat", N)
for i := 0; i < N; i++ {
bits := uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
x := math.Float64frombits(bits)
shortFast := FormatFloat(x, 'g', -1, 64)
SetOptimize(false)
shortSlow := FormatFloat(x, 'g', -1, 64)
SetOptimize(true)
if shortSlow != shortFast {
t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
}
prec := rand.Intn(12) + 5
shortFast = FormatFloat(x, 'e', prec, 64)
SetOptimize(false)
shortSlow = FormatFloat(x, 'e', prec, 64)
SetOptimize(true)
if shortSlow != shortFast {
t.Errorf("%b printed as %s, want %s", x, shortFast, shortSlow)
}
}
}
示例3: packet
func packet(raddr net.IP) []byte {
ip := &layers.IPv4{
Version: 0x4,
TOS: 0x0,
TTL: 0x40,
Protocol: layers.IPProtocolTCP,
SrcIP: net.ParseIP(os.Args[2]),
DstIP: raddr,
WithRawINETSocket: true,
}
rand.Seed(time.Now().UnixNano())
tcp := &layers.TCP{
SrcPort: layers.TCPPort(rand.Uint32()),
DstPort: 0x50,
Seq: rand.Uint32(),
DataOffset: 0x5,
SYN: true,
Window: 0xaaaa,
}
tcp.SetNetworkLayerForChecksum(ip)
buf := gopacket.NewSerializeBuffer()
opts := gopacket.SerializeOptions{true, true}
check(gopacket.SerializeLayers(buf, opts, ip, tcp))
return buf.Bytes()
}
示例4: TestGobNormal
func TestGobNormal(t *testing.T) {
h, err := NewHLL(10)
assert.Nil(t, err)
h.ToNormal()
var i float64
for i = 0; i <= 100000; i++ {
h.Add(fmt.Sprintf("%d-%d", i, rand.Uint32()))
}
assert.Equal(t, h.format, NORMAL, "Not using normal mode")
c := h.Cardinality()
errorRate := 1.04 / math.Sqrt(float64(h.m1))
checkErrorBounds(t, c, i, errorRate)
var buf bytes.Buffer
err = gob.NewEncoder(&buf).Encode(h)
assert.Nil(t, err)
var h2 HLL
err = gob.NewDecoder(&buf).Decode(&h2)
assert.Nil(t, err)
assert.Equal(t, h2.format, NORMAL, "Not using normal mode")
assert.Equal(t, c, h2.Cardinality())
for i = 0; i <= 40000; i++ {
v := rand.Uint32()
h.Add(fmt.Sprintf("%d-%d", i, v))
h2.Add(fmt.Sprintf("%d-%d", i, v))
}
assert.Equal(t, h.Cardinality(), h2.Cardinality())
}
示例5: KeyPair
func KeyPair() (privateKey, publicKey uint64) {
a := uint64(rand.Uint32())
b := uint64(rand.Uint32()) + 1
privateKey = (a << 32) | b
publicKey = PublicKey(privateKey)
return
}
示例6: generateTransactionId
func generateTransactionId() [3]uint32 {
return [3]uint32{
rand.Uint32(),
rand.Uint32(),
rand.Uint32(),
}
}
示例7: spammer
func spammer() {
hostname := "SpamSpamSpam SpamSpamSpamSpamSpamSpamSpamSpamSpamSpamSpamSpam"
broadcastConn := openUDPConnOrDie(spamerPort)
defer broadcastConn.Close()
addr, err := net.ResolveUDPAddr("udp4", net.IPv4bcast.String()+":"+strconv.Itoa(receiverPort))
if err != nil {
log.Fatalf("Cannot resolve broadcast receiver address: %s", err)
}
hardwareAddr := make([]byte, 6)
broadcastCommonData := append(hardwareAddr, byte(len(hostname)))
broadcastCommonData = append(broadcastCommonData, ([]byte)(hostname)...)
timestampBuff := make([]byte, 4) //32!!!!
ticker := time.NewTicker(spamInterval)
for _ = range ticker.C {
unixTimestamp := Timestamp(time.Now().Unix())
endian.PutUint32(timestampBuff, uint32(unixTimestamp))
endian.PutUint32(broadcastCommonData, rand.Uint32())
endian.PutUint32(broadcastCommonData[2:], rand.Uint32())
copy(broadcastCommonData[7:], []byte(net.HardwareAddr(broadcastCommonData[0:6]).String()))
_, err = broadcastConn.WriteTo(append(broadcastCommonData, timestampBuff...), addr)
if err != nil {
log.Fatalf("Unable to broadcast SPAM: %s", err)
}
}
}
示例8: CheapRandString16
func CheapRandString16() string {
if !seeded {
rand.Seed(time.Now().UnixNano())
seeded = true
}
return fmt.Sprintf("%x%x", rand.Uint32(), rand.Uint32())
}
示例9: TestReadWriteAtom
func TestReadWriteAtom(t *testing.T) {
ctx := NewCtx(1)
a := ctx.newAtom()
copy(a.data, []byte{'a'})
testReadWriteAtom(t, a)
copy(a.data, []byte{'0'})
testReadWriteAtom(t, a)
copy(a.data, []byte{'\x00'})
testReadWriteAtom(t, a)
for i := 0; i < 100; i++ {
copy(a.data, []byte{byte(mathrand.Uint32())})
testReadWriteAtom(t, a)
}
ctx2 := NewCtx(2)
a2 := ctx2.newAtom()
copy(a2.data, []byte{'a', 'a'})
testReadWriteAtom(t, a)
copy(a2.data, []byte{'0', '0'})
testReadWriteAtom(t, a)
copy(a2.data, []byte{'\x00', '\x00'})
testReadWriteAtom(t, a)
for i := 0; i < 100; i++ {
r := mathrand.Uint32()
copy(a.data, []byte{byte(r), byte(r >> 8)})
testReadWriteAtom(t, a)
}
}
示例10: intPairArray
func intPairArray(n int) []KeyValPair {
a := make([]KeyValPair, n)
for i := 0; i < n; i++ {
a[i] = KeyValPair{rand.Uint32(), rand.Uint32()}
}
return a
}
示例11: CreateClient
func CreateClient(uid uint64, password string) {
RECONNECTION:
//tcpAddr, err := net.ResolveTCPAddr("tcp", "192.168.20.51:9100")
tcpAddr, err := net.ResolveTCPAddr("tcp", "112.74.66.141:9100")
//tcpAddr, err := net.ResolveTCPAddr("tcp", "112.74.66.141:9100")
//tcpAddr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:9100")
if err != nil {
fmt.Println(err)
return
}
clientIP := fmt.Sprintf("127.%v.%v.%v:0", rand.Uint32()%255, rand.Uint32()%255, rand.Uint32()%255)
locAddr, err := net.ResolveTCPAddr("tcp", clientIP)
if err != nil {
panic(err)
}
connClient, err := net.DialTCP("tcp", locAddr, tcpAddr)
if err != nil {
fmt.Println(err)
return
}
client := &Client{
conn: connClient,
uid: uid,
}
log.Println("connect start:", *client)
err = client.recvPkg()
if err != nil && err != io.EOF {
log.Println(err, *client)
return
goto RECONNECTION
} else if err != nil {
panic(err)
}
log.Println("connect end:", *client)
// 先登录
client.execArgs("10102", uid, password)
client.recvPkg()
cur_count++
go client.run()
//select {}
time.Sleep(3 * time.Minute)
for {
// 跑业务
//second := time.Duration(rand.Uint32()%150) + 30
time.Sleep(time.Second * 60)
if client.sid == 0 {
continue
}
uid := int(rand.Uint32())%cur_count + MIN_UID
g_rwMutex.RLock()
_, ok := g_client_map[uint64(uid)]
g_rwMutex.RUnlock()
if ok {
client.execArgs("30101", uid)
}
}
client.Close()
}
示例12: testDuelingProposal
func testDuelingProposal(doneChan chan bool) {
key := "a"
randint, _ := crand.Int(crand.Reader, big.NewInt(math.MaxInt64))
rand.Seed(randint.Int64())
value1 := rand.Uint32()
value2 := rand.Uint32()
if _, ok := pt.cliMap[1]; !ok {
LOGE.Println("FAIL: missing node 1")
doneChan <- false
return
}
// retChan := make(chan *paxosrpc.ProposeReply, 10)
pnum1, err1 := pt.GetNextProposalNumber(key, 1)
pnum2, err2 := pt.GetNextProposalNumber(key, 1)
fmt.Println("Proposing:", key, value1, pnum1.N)
// reply1, err := pt.ProposeForDueling(key, value1, pnum1.N, 1, retChan)
reply1, err1 := pt.Propose(key, value1, pnum1.N, 1)
fmt.Println(reply1.V)
fmt.Println("Proposing:", key, value2, pnum2.N)
reply2, err2 := pt.Propose(key, value2, pnum2.N, 1)
// reply2, err := pt.ProposeForDueling(key, value2, pnum2.N, 1, retChan)
fmt.Println(reply2.V)
if err1 != nil {
printFailErr("Propose", err1)
doneChan <- false
return
}
if err2 != nil {
printFailErr("Propose", err2)
doneChan <- false
return
}
if !checkProposeReply(reply1, key, value1) {
doneChan <- false
return
}
fmt.Println("check 1 pass")
if !checkGetValueAll(key, value2) {
doneChan <- false
return
}
// if !checkProposeReply(reply2, key, value2) {
// doneChan <- false
// return
// }
// if !checkGetValueAll(key, value2) {
// doneChan <- false
// return
// }
doneChan <- true
}
示例13: BenchmarkMap
func (s *PostingSuite) BenchmarkMap(c *C) {
postings := make(fakePostings, c.N)
for i := 0; i <= c.N; i++ {
doctype := rand.Uint32()%100 + 1
docid := rand.Uint32()%100 + 1
postings.Add(doctype, docid)
}
}
示例14: BenchmarkWriteControl
func BenchmarkWriteControl(b *testing.B) {
b.RunParallel(func(pb *testing.PB) {
for pb.Next() {
rand.Uint32()
rand.Uint32()
}
})
}
示例15: TestEncodeBigInt
func TestEncodeBigInt(t *testing.T) {
assertEnc(t, hexChars, 15, "f")
assertEnc(t, hexChars, 16, "10")
for i := 0; i < 1000; i++ {
assertSync(t, hexChars, rand.Uint32())
assertSync(t, flumChars, rand.Uint32())
}
}