Skip to content

Commit

Permalink
Use the current time for initializing RNG.x instead of slow and poten…
Browse files Browse the repository at this point in the history
…tially blocking crypto/rand
  • Loading branch information
valyala committed May 31, 2017
1 parent ad48af2 commit 19dd0f0
Showing 1 changed file with 3 additions and 8 deletions.
11 changes: 3 additions & 8 deletions fastrand.go
Expand Up @@ -6,9 +6,8 @@
package fastrand

import (
cryptorand "crypto/rand"
"fmt"
"sync"
"time"
)

// Uint32 returns pseudorandom uint32.
Expand Down Expand Up @@ -70,10 +69,6 @@ func (r *RNG) Uint32n(maxN uint32) uint32 {
}

func getRandomUint32() uint32 {
var buf [4]byte
_, err := cryptorand.Read(buf[:])
if err != nil {
panic(fmt.Sprintf("BUG: cannot read random number: %s", err))
}
return uint32(buf[3]) | (uint32(buf[2]) << 8) | (uint32(buf[1]) << 16) | (uint32(buf[0]) << 24)
x := time.Now().UnixNano()
return uint32((x >> 32) ^ x)
}

0 comments on commit 19dd0f0

Please sign in to comment.