From 21bbf9e63219a551771139ea7494c8c1fbe41a4a Mon Sep 17 00:00:00 2001 From: Angelo Capossele <angelocapossele@gmail.com> Date: Thu, 14 May 2020 10:13:50 +0100 Subject: [PATCH] Fix entropy of prng (#422) * :bug: fix entropy of prng * :recycle: simplify pseudoR --- packages/prng/unix_ts_rng.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/packages/prng/unix_ts_rng.go b/packages/prng/unix_ts_rng.go index 1cfcc683..58c212e3 100644 --- a/packages/prng/unix_ts_rng.go +++ b/packages/prng/unix_ts_rng.go @@ -1,8 +1,7 @@ package prng import ( - "bytes" - "encoding/binary" + "math/rand" "time" ) @@ -62,12 +61,10 @@ func (utrng *UnixTimestampPrng) send() { now := utrng.timeSourceFunc() // reduce to last resolution timePoint := now - (now % utrng.resolution) - // convert to float64 - buf := bytes.NewBuffer(make([]byte, 0, 8)) - if err := binary.Write(buf, binary.LittleEndian, timePoint); err != nil { - panic(err) - } - pseudoR := float64(binary.BigEndian.Uint64(buf.Bytes()[:8])>>11) / (1 << 53) + + // add entropy and convert to float64 + pseudoR := rand.New(rand.NewSource(timePoint)).Float64() + // skip slow consumers select { case utrng.c <- pseudoR: -- GitLab