You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

306 lines
5.1 KiB

9 years ago
9 years ago
7 years ago
7 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package common
  2. import (
  3. crand "crypto/rand"
  4. mrand "math/rand"
  5. "sync"
  6. "time"
  7. )
  8. const (
  9. strChars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz" // 62 characters
  10. )
  11. // Rand is a prng, that is seeded with OS randomness.
  12. // The OS randomness is obtained from crypto/rand, however none of the provided
  13. // methods are suitable for cryptographic usage.
  14. // They all utilize math/rand's prng internally.
  15. //
  16. // All of the methods here are suitable for concurrent use.
  17. // This is achieved by using a mutex lock on all of the provided methods.
  18. type Rand struct {
  19. sync.Mutex
  20. rand *mrand.Rand
  21. }
  22. var grand *Rand
  23. func init() {
  24. grand = NewRand()
  25. grand.init()
  26. }
  27. func NewRand() *Rand {
  28. rand := &Rand{}
  29. rand.init()
  30. return rand
  31. }
  32. func (r *Rand) init() {
  33. bz := cRandBytes(8)
  34. var seed uint64
  35. for i := 0; i < 8; i++ {
  36. seed |= uint64(bz[i])
  37. seed <<= 8
  38. }
  39. r.reset(int64(seed))
  40. }
  41. func (r *Rand) reset(seed int64) {
  42. r.rand = mrand.New(mrand.NewSource(seed))
  43. }
  44. //----------------------------------------
  45. // Global functions
  46. func Seed(seed int64) {
  47. grand.Seed(seed)
  48. }
  49. func RandStr(length int) string {
  50. return grand.Str(length)
  51. }
  52. func RandUint16() uint16 {
  53. return grand.Uint16()
  54. }
  55. func RandUint32() uint32 {
  56. return grand.Uint32()
  57. }
  58. func RandUint64() uint64 {
  59. return grand.Uint64()
  60. }
  61. func RandUint() uint {
  62. return grand.Uint()
  63. }
  64. func RandInt16() int16 {
  65. return grand.Int16()
  66. }
  67. func RandInt32() int32 {
  68. return grand.Int32()
  69. }
  70. func RandInt64() int64 {
  71. return grand.Int64()
  72. }
  73. func RandInt() int {
  74. return grand.Int()
  75. }
  76. func RandInt31() int32 {
  77. return grand.Int31()
  78. }
  79. func RandInt31n(n int32) int32 {
  80. return grand.Int31n(n)
  81. }
  82. func RandInt63() int64 {
  83. return grand.Int63()
  84. }
  85. func RandInt63n(n int64) int64 {
  86. return grand.Int63n(n)
  87. }
  88. func RandBool() bool {
  89. return grand.Bool()
  90. }
  91. func RandFloat32() float32 {
  92. return grand.Float32()
  93. }
  94. func RandFloat64() float64 {
  95. return grand.Float64()
  96. }
  97. func RandTime() time.Time {
  98. return grand.Time()
  99. }
  100. func RandBytes(n int) []byte {
  101. return grand.Bytes(n)
  102. }
  103. func RandIntn(n int) int {
  104. return grand.Intn(n)
  105. }
  106. func RandPerm(n int) []int {
  107. return grand.Perm(n)
  108. }
  109. //----------------------------------------
  110. // Rand methods
  111. func (r *Rand) Seed(seed int64) {
  112. r.Lock()
  113. r.reset(seed)
  114. r.Unlock()
  115. }
  116. // Str constructs a random alphanumeric string of given length.
  117. func (r *Rand) Str(length int) string {
  118. chars := []byte{}
  119. MAIN_LOOP:
  120. for {
  121. val := r.Int63()
  122. for i := 0; i < 10; i++ {
  123. v := int(val & 0x3f) // rightmost 6 bits
  124. if v >= 62 { // only 62 characters in strChars
  125. val >>= 6
  126. continue
  127. } else {
  128. chars = append(chars, strChars[v])
  129. if len(chars) == length {
  130. break MAIN_LOOP
  131. }
  132. val >>= 6
  133. }
  134. }
  135. }
  136. return string(chars)
  137. }
  138. func (r *Rand) Uint16() uint16 {
  139. return uint16(r.Uint32() & (1<<16 - 1))
  140. }
  141. func (r *Rand) Uint32() uint32 {
  142. r.Lock()
  143. u32 := r.rand.Uint32()
  144. r.Unlock()
  145. return u32
  146. }
  147. func (r *Rand) Uint64() uint64 {
  148. return uint64(r.Uint32())<<32 + uint64(r.Uint32())
  149. }
  150. func (r *Rand) Uint() uint {
  151. r.Lock()
  152. i := r.rand.Int()
  153. r.Unlock()
  154. return uint(i)
  155. }
  156. func (r *Rand) Int16() int16 {
  157. return int16(r.Uint32() & (1<<16 - 1))
  158. }
  159. func (r *Rand) Int32() int32 {
  160. return int32(r.Uint32())
  161. }
  162. func (r *Rand) Int64() int64 {
  163. return int64(r.Uint64())
  164. }
  165. func (r *Rand) Int() int {
  166. r.Lock()
  167. i := r.rand.Int()
  168. r.Unlock()
  169. return i
  170. }
  171. func (r *Rand) Int31() int32 {
  172. r.Lock()
  173. i31 := r.rand.Int31()
  174. r.Unlock()
  175. return i31
  176. }
  177. func (r *Rand) Int31n(n int32) int32 {
  178. r.Lock()
  179. i31n := r.rand.Int31n(n)
  180. r.Unlock()
  181. return i31n
  182. }
  183. func (r *Rand) Int63() int64 {
  184. r.Lock()
  185. i63 := r.rand.Int63()
  186. r.Unlock()
  187. return i63
  188. }
  189. func (r *Rand) Int63n(n int64) int64 {
  190. r.Lock()
  191. i63n := r.rand.Int63n(n)
  192. r.Unlock()
  193. return i63n
  194. }
  195. func (r *Rand) Float32() float32 {
  196. r.Lock()
  197. f32 := r.rand.Float32()
  198. r.Unlock()
  199. return f32
  200. }
  201. func (r *Rand) Float64() float64 {
  202. r.Lock()
  203. f64 := r.rand.Float64()
  204. r.Unlock()
  205. return f64
  206. }
  207. func (r *Rand) Time() time.Time {
  208. return time.Unix(int64(r.Uint64()), 0)
  209. }
  210. // Bytes returns n random bytes generated from the internal
  211. // prng.
  212. func (r *Rand) Bytes(n int) []byte {
  213. // cRandBytes isn't guaranteed to be fast so instead
  214. // use random bytes generated from the internal PRNG
  215. bs := make([]byte, n)
  216. for i := 0; i < len(bs); i++ {
  217. bs[i] = byte(r.Int() & 0xFF)
  218. }
  219. return bs
  220. }
  221. // Intn returns, as an int, a uniform pseudo-random number in the range [0, n).
  222. // It panics if n <= 0.
  223. func (r *Rand) Intn(n int) int {
  224. r.Lock()
  225. i := r.rand.Intn(n)
  226. r.Unlock()
  227. return i
  228. }
  229. // Bool returns a uniformly random boolean
  230. func (r *Rand) Bool() bool {
  231. // See https://github.com/golang/go/issues/23804#issuecomment-365370418
  232. // for reasoning behind computing like this
  233. return r.Int63()%2 == 0
  234. }
  235. // Perm returns a pseudo-random permutation of n integers in [0, n).
  236. func (r *Rand) Perm(n int) []int {
  237. r.Lock()
  238. perm := r.rand.Perm(n)
  239. r.Unlock()
  240. return perm
  241. }
  242. // NOTE: This relies on the os's random number generator.
  243. // For real security, we should salt that with some seed.
  244. // See github.com/tendermint/tendermint/crypto for a more secure reader.
  245. func cRandBytes(numBytes int) []byte {
  246. b := make([]byte, numBytes)
  247. _, err := crand.Read(b)
  248. if err != nil {
  249. PanicCrisis(err)
  250. }
  251. return b
  252. }