github.com/iotexproject/iotex-core@v1.14.1-rc1/pkg/fastrand/fastrand.go (about) 1 // Copyright (c) 2022 IoTeX Foundation 2 // This source code is provided 'as is' and no warranties are given as to title or non-infringement, merchantability 3 // or fitness for purpose and, to the extent permitted by law, all liability for your use of the code is disclaimed. 4 // This source code is governed by Apache License 2.0 that can be found in the LICENSE file. 5 6 package fastrand 7 8 import ( 9 _ "unsafe" // for go link runtime fastrand 10 ) 11 12 // Uint32 returns a random 32-bit 13 // 14 //go:linkname Uint32 runtime.fastrand 15 func Uint32() uint32 16 17 // Uint32n returns a random 32-bit in the range [0..n). 18 // 19 //go:linkname Uint32n runtime.fastrandn 20 func Uint32n(n uint32) uint32 21 22 // Read generates len(p) random bytes and writes them into p 23 func Read(p []byte) (n int) { 24 for n < len(p) { 25 val := Uint32() 26 i := 0 27 for ; i < 4; i++ { 28 if n+i > len(p)-1 { 29 break 30 } 31 p[n+i] = byte(val) 32 val >>= 8 33 } 34 n = n + i 35 } 36 return 37 }