From d66d43d2eaa5d2611ed1e2814b1df7bf01df705a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Corbi=C3=A8re?= Date: Tue, 3 Apr 2018 16:23:36 +0200 Subject: [PATCH 1/2] Add Int31n() and RandInt31n(). --- common/random.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/common/random.go b/common/random.go index b767e3589..5653142eb 100644 --- a/common/random.go +++ b/common/random.go @@ -93,6 +93,10 @@ func RandInt31() int32 { return grand.Int31() } +func RandInt31n(n int32) int32 { + return grand.Int31n(n) +} + func RandInt63() int64 { return grand.Int63() } @@ -224,6 +228,14 @@ func (r *Rand) Int31() int32 { return i31 } +// It is not safe for cryptographic usage. +func (r *Rand) Int31n(n int32) int32 { + r.Lock() + i31n := r.rand.Int31n(n) + r.Unlock() + return i31n +} + // It is not safe for cryptographic usage. func (r *Rand) Int63() int64 { r.Lock() From 74486f7f932a7ec2c54f55e2b0319c621b2d5bdf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thomas=20Corbi=C3=A8re?= Date: Tue, 3 Apr 2018 16:24:38 +0200 Subject: [PATCH 2/2] Add Int63n() and RandInt63n(). --- common/random.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/common/random.go b/common/random.go index 5653142eb..389a32fc2 100644 --- a/common/random.go +++ b/common/random.go @@ -101,6 +101,10 @@ func RandInt63() int64 { return grand.Int63() } +func RandInt63n(n int64) int64 { + return grand.Int63n(n) +} + func RandUint16Exp() uint16 { return grand.Uint16Exp() } @@ -244,6 +248,14 @@ func (r *Rand) Int63() int64 { return i63 } +// It is not safe for cryptographic usage. +func (r *Rand) Int63n(n int64) int64 { + r.Lock() + i63n := r.rand.Int63n(n) + r.Unlock() + return i63n +} + // Distributed pseudo-exponentially to test for various cases // It is not safe for cryptographic usage. func (r *Rand) Uint16Exp() uint16 {