示例#1
0
文件: divconst.go 项目: 8l/go-learn
func
u64rand() uint64
{
	a := uint64(rand.Uint32());
	a = (a<<32) | uint64(rand.Uint32());
	a >>= uint(rand.Intn(64));
	return a;
}
示例#2
0
文件: demo.go 项目: shellreef/gones
// Continuously update the screen with something interesting
func render() {
	counter := 0
	for {
		w, h := leggo.Dimensions()

		for y := 0; y < h; y += 1 {
			for x := 0; x < w; x += 1 {
				var r, g, b byte
				// Some random things to look at
				switch mode % 8 {
				case 0:
					r = byte(rand.Uint32())
					g = r
					b = r
				case 1:
					r = byte(rand.Uint32())
					g = byte(rand.Uint32())
					b = byte(rand.Uint32())
				case 2:
					r = byte(x * y)
				case 3:
					r = byte(x*y - int(rand.Uint32())%50)
				case 4:
					if x == counter%w {
						r = 255
						g = 255
						b = 255
					}
				case 5:
					if counter%2 == 1 {
						g = 255
					} else {
						r = 255
					}
					time.Sleep(10000)
				case 6:
					if x == w/2 {
						g = 255
					}
					if y == h/2 {
						r = 255
					}
				case 7:
					if x == y {
						b = 255
					}
				}

				leggo.WritePixel(x, y, r, g, b, 0)
			}
		}
		counter += 1
	}
}
示例#3
0
func setupHash(size int) {
	emptyBoard[size] = make([]Hash, size*size)
	blackBoard[size] = make([]Hash, size*size)
	whiteBoard[size] = make([]Hash, size*size)
	rand.Seed(int64(size))
	for i := 0; i < size*size; i++ {
		emptyBoard[size][i] = Hash(rand.Uint32())
		blackBoard[size][i] = Hash(rand.Uint32())
		whiteBoard[size][i] = Hash(rand.Uint32())
	}
}
示例#4
0
func i64rand() int64 {
	for {
		a := int64(rand.Uint32())
		a = (a << 32) | int64(rand.Uint32())
		a >>= uint(rand.Intn(64))
		if -a != a {
			return a
		}
	}
	return 0 // impossible
}
示例#5
0
func main() {
	if sdl.Init(sdl.INIT_VIDEO) != 0 {
		panic(sdl.GetError())
	}

	defer sdl.Quit()

	screen := sdl.SetVideoMode(400, 300, 32, 0)
	if screen == nil {
		panic(sdl.GetError())
	}

	sdl.WM_SetCaption("Template", "")

	ticker := time.NewTicker(1e9 / 2 /*2 Hz*/)

loop:
	for {
		select {
		case <-ticker.C:
			// Note: For better efficiency, use UpdateRects instead of Flip
			screen.FillRect(nil /*color*/, rand.Uint32())
			//screen.Blit(&sdl.Rect{x,y, 0, 0}, image, nil)
			screen.Flip()

		case event := <-sdl.Events:
			fmt.Printf("%#v\n", event)

			switch e := event.(type) {
			case sdl.QuitEvent:
				break loop
			}
		}
	}
}
示例#6
0
文件: divconst.go 项目: 8l/go-learn
func
u8rand() uint8
{
	a := uint8(rand.Uint32());
	a >>= uint(rand.Intn(8));
	return a;
}
示例#7
0
func main() {
	flag.Parse()
	//	prime();
	var blocks [1]struct {
		base *byte
		siz  uintptr
	}
	for i := 0; i < 1<<10; i++ {
		if i%(1<<10) == 0 && *chatty {
			println(i)
		}
		b := rand.Int() % len(blocks)
		if blocks[b].base != nil {
			//	println("Free", blocks[b].siz, blocks[b].base);
			runtime.Free(blocks[b].base)
			blocks[b].base = nil
			allocated -= uint64(blocks[b].siz)
			continue
		}
		siz := uintptr(rand.Int() >> (11 + rand.Uint32()%20))
		base := runtime.Alloc(siz)
		//	ptr := uintptr(syscall.BytePtr(base))+uintptr(siz/2);
		//	obj, size, ref, ok := allocator.find(ptr);
		//	if obj != base || *ref != 0 || !ok {
		//		panicln("find", siz, obj, ref, ok);
		//	}
		blocks[b].base = base
		blocks[b].siz = siz
		allocated += uint64(siz)
		//	println("Alloc", siz, base);
		memset(base, 0xbb, siz)
		bigger()
	}
}
示例#8
0
文件: divconst.go 项目: 8l/go-learn
func
u32rand() uint32
{
	a := uint32(rand.Uint32());
	a >>= uint(rand.Intn(32));
	return a;
}
示例#9
0
文件: message.go 项目: vonwenm/GoDHCP
func NewMessage(xid uint32, op, hwtype byte, hwaddr []byte, C, Y, S, G net.IP) (msg *Message, err os.Error) {
	// select a random XID
	if xid == 0 {
		xid = rand.Uint32()
	}
	if len(hwaddr) > 16 {
		err = os.NewError("Invalid DHCP hardware address (too long)")
		return
	}
	msg = &Message{
		Operation:    op,
		HardwareType: hwtype,
		HardwareLen:  byte(len(hwaddr)),
		Xid:          xid,
		// Secs: 0?
		// We default to FLAG_BROADCAST to avoid needing raw packets in Linux
		// This works for EC2, ISC, and possibly others, but no idea where else.
		Flags:     FLAG_BROADCAST,
		ClientIP:  C,
		YourIP:    Y,
		ServerIP:  S,
		GatewayIP: G,
	}
	copy(msg.ClientHWAddr[0:], hwaddr)
	return
}
示例#10
0
文件: divconst.go 项目: 8l/go-learn
func
u16rand() uint16
{
	a := uint16(rand.Uint32());
	a >>= uint(rand.Intn(16));
	return a;
}
示例#11
0
func TestStore(t *testing.T) {
	bloom := New()

	for i := 0; i < STORE_RUNS; i++ {
		bloom.Add(string(rand.Uint32()))
	}
}
示例#12
0
文件: rsa.go 项目: JackeLee/drobnosti
func randBytes(len int) []byte {
	bytes := make([]byte, len)
	for i := len - 1; i >= 0; i-- {
		bytes[i] = byte(rand.Uint32() % 256)
	}
	return bytes
}
示例#13
0
// Create a random string
func randString(strLen int) (randStr string) {
	strChars := "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
	for i := 0; i < strLen; i++ {
		randUint := rand.Uint32()
		pos := randUint % uint32(len(strChars))
		randStr += string(strChars[pos])
	}
	return
}
示例#14
0
文件: files_test.go 项目: jacobsa/igo
func createTempDir() string {
	once.Do(seedRand)
	result := fmt.Sprintf("/tmp/files_test.%d", rand.Uint32())
	err := os.Mkdir(result, 0700)
	if err != nil {
		panic(fmt.Sprintf("Can't create dir [%s]: %s", result, err))
	}

	return result
}
示例#15
0
func i16rand() int16 {
	for {
		a := int16(rand.Uint32())
		a >>= uint(rand.Intn(16))
		if -a != a {
			return a
		}
	}
	return 0 // impossible
}
示例#16
0
func i8rand() int8 {
	for {
		a := int8(rand.Uint32())
		a >>= uint(rand.Intn(8))
		if -a != a {
			return a
		}
	}
	return 0 // impossible
}
示例#17
0
文件: rsa.go 项目: JackeLee/drobnosti
// Vygeneruje nahodne cislo v intervalu [2^len, 2^len+1)
func RandNum(len int) *big.Int {
	bytes := randBytes((len + 7) / 8)
	s := byte(len % 8)
	bytes[0] %= 2 << s
	for bytes[0] == 0 {
		bytes[0] = byte(rand.Uint32() % (2 << s))
	}

	return new(big.Int).SetBytes(bytes)
}
示例#18
0
func i32rand() int32 {
	for {
		a := int32(rand.Uint32())
		a >>= uint(rand.Intn(32))
		if -a != a {
			return a
		}
	}
	return 0 // impossible
}
示例#19
0
文件: gotris.go 项目: sonald/gotris
func NewRandomTetrisFigureNot(figure *TetrisFigure) *TetrisFigure {
	var ri uint32
	for {
		ri = rand.Uint32() % uint32(len(specs))
		if ri != figure.Class {
			break
		}
	}
	f := NewTetrisFigure(specs[ri], specColors[ri])
	f.Class = ri
	return f
}
示例#20
0
func chooseID() bson.ObjectId {
	rand.Seed(time.Nanoseconds())
	b := make([]byte, IDLEN)
	for i := 0; i < IDLEN/4; i++ {
		u := rand.Uint32()
		b[4*i] = byte(u & 0xff)
		b[4*i+1] = byte((u >> 8) & 0xff)
		b[4*i+2] = byte((u >> 16) & 0xff)
		b[4*i+3] = byte((u >> 24) & 0xff)
	}
	return bson.ObjectId(b)
}
示例#21
0
func TestRetrieveFalse(t *testing.T) {
	bloom := New()

	var toTest [RETRIEVE_RUNS]string
	for i := 0; i < RETRIEVE_RUNS; i++ {
		toTest[i] = string(rand.Uint32())
	}

	for i := 0; i < RETRIEVE_RUNS; i++ {
		if bloom.In(toTest[i]) {
			t.Error("BloomFilter.In returns false negative for string ", toTest[i])
		}
	}
}
示例#22
0
func TestRetrieveTrue(t *testing.T) {
	bloom := New()

	var toAdd [RETRIEVE_RUNS]string
	for i := 0; i < RETRIEVE_RUNS; i++ {
		toAdd[i] = string(rand.Uint32())
		bloom.Add(toAdd[i])
	}
	for i := 0; i < RETRIEVE_RUNS; i++ {
		if !bloom.In(toAdd[i]) {
			t.Error("BloomFilter.In returns false posative for string ", toAdd[i])
		}
	}
}
示例#23
0
func main() {
	flag.Parse()
	log.Printf("Server starting on port %d\n", *portnum)
	if *nodeID == 0 {
		rand.Seed(time.Nanoseconds())
		*nodeID = uint(rand.Uint32())
	}
	ss := storageserver.NewStorageserver(*storageMasterNodePort, *numNodes, *portnum, uint32(*nodeID))
	ts := NewTribserver(ss)
	rpc.Register(ts)
	srpc := storagerpc.NewStorageRPC(ss)
	rpc.Register(srpc)
	rpc.HandleHTTP()
	l, e := net.Listen("tcp", fmt.Sprintf(":%d", *portnum))
	if e != nil {
		log.Fatal("listen error:", e)
	}
	log.Printf("Establishing connection with storage servers")
	go ss.Connect(l.Addr())
	http.Serve(l, nil)
}
示例#24
0
文件: server.go 项目: pcgod/grumble
// Generate a random, valid session ID.
// The returned session ID is guaranteed not to currently be in use
// on the server, and not to be the zero-value for integers (0).
func (server *Server) GenSessionId() (session uint32) {
	for {
		session = rand.Uint32()

		// The 0 session is disallowed in Grumble. 0 is the zero-value for
		// integer types, and as such, an uninitialized session id could
		// point to a valid client if we allowed the 0 session id.
		if session == 0 {
			continue
		}

		// Is there already a client on the sever with the generated id?
		// Give us a new one, please.
		if _, exists := server.clients[session]; exists {
			continue
		}

		break
	}

	return
}
示例#25
0
func (cm *CallManager) Call(addr net.Addr, name string, args []interface{}) (retis []interface{}, err os.Error) {

	cm.logger.Logf("call called\n")
	fmt.Printf("CALL\n")

	rpc := new(RPC)
	header := new(Header)
	rpc.Header = header

	header.Version = 1
	header.Sender = cm.Id
	// TODO: DHTId!
	header.DHTId = nil
	header.Id = uint64(rand.Uint32())<<32 | uint64(rand.Uint32())
	header.Call = 0x01

	packet := new(Packet)
	rpc.Packet = packet
	packet.To = addr

	rpcframe := new(RPCFrame)
	rpc.RPCFrame = rpcframe
	rpcframe.Name = name
	rpcframe.OutArgs = args

	rpcdesc := make(map[string]interface{})
	rpcdesc["name"] = name
	rpcdesc["args"] = args

	rpc.Payload = rpcdesc

	cm.logger.Logf("Call: encoding packet")
	data := cm.EncodeRPC(rpc)
	if packet == nil {
		return nil, RPCError("could not encode packet")
	}
	packet.Data = data

	// register RPC...
	running := &RunningRPC{rpc, make(chan *RPC)}
	cm.regchan <- running

	err = cm.transceiver.SendRPC(rpc)
	if err != nil {
		return nil, err
	}

	retrpc := <-running.retchan
	if retrpc == nil {
		return nil, RPCError("time out")
	}

	if retis, ok := retrpc.Payload.([]interface{}); ok {
		return retis, nil
	}

	if cm.log {
		cm.logger.Logf("return payload was not []interface{}\n")
	}
	return nil, nil

}
示例#26
0
文件: gotris.go 项目: sonald/gotris
func NewRandomTetrisFigure() *TetrisFigure {
	ri := rand.Uint32() % uint32(len(specs))
	f := NewTetrisFigure(specs[ri], specColors[ri])
	f.Class = ri
	return f
}