Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
kelindar committed Sep 11, 2021
1 parent 9c2fe18 commit 3349083
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 14 deletions.
21 changes: 15 additions & 6 deletions rand.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package rand

import (
"math"
"math/bits"
"runtime"
"sync/atomic"
Expand All @@ -14,7 +13,7 @@ import (

// Int32 returns a tread-safe, non-cryptographic pseudorandom int32.
func Int32() int32 {
return int32(xxhash32(uint32(next()), 0))
return int32(xxhash32(uint32(next()), 0) >> 1)
}

// Int31n returns, as an int32, a non-negative pseudo-random number in the half-open interval [0,n)
Expand All @@ -41,7 +40,7 @@ func Uint32n(maxN uint32) uint32 {

// Int63 returns a non-negative pseudo-random 63-bit integer as an int64
func Int63() int64 {
return int64(xxhash64(next(), 0))
return int64(xxhash64(next(), 0) >> 1)
}

// Int63n returns, as an int64, a non-negative pseudo-random number in the half-open interval [0,n). It panics if n <= 0.
Expand Down Expand Up @@ -84,12 +83,22 @@ func Bool() bool {

// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0)
func Float32() float32 {
return math.Float32frombits(Uint32()) / (1 << 31)
again: // Source: math/rand. Copyright 2009 The Go Authors.
f := float32(Float64())
if f == 1 {
goto again
}
return f
}

// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0)
// Float64 returns, as a float64, a pseudo-random number in the half-open interval [0.0,1.0).
func Float64() float64 {
return math.Float64frombits(Uint64()) / (1 << 63)
again: // Source: math/rand. Copyright 2009 The Go Authors.
f := float64(Int63()) / (1 << 63)
if f == 1 {
goto again
}
return f
}

// --------------------------------- Hashing ---------------------------------
Expand Down
18 changes: 10 additions & 8 deletions rand_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,9 @@ func TestInt32(t *testing.T) {
m := make(map[int32]struct{})
for i := 0; i < 1e3; i++ {
n := Int32()
assert.GreaterOrEqual(t, n, int32(0))
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
assert.Fail(t, "number %v already exists", n)
}
m[n] = struct{}{}
}
Expand All @@ -105,8 +106,9 @@ func TestInt63(t *testing.T) {
m := make(map[int64]struct{})
for i := 0; i < 1e3; i++ {
n := Int63()
assert.GreaterOrEqual(t, n, int64(0))
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
assert.Fail(t, "number %v already exists", n)
}
m[n] = struct{}{}
}
Expand All @@ -117,7 +119,7 @@ func TestUint32(t *testing.T) {
for i := 0; i < 1e3; i++ {
n := Uint32()
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
assert.Fail(t, "number %v already exists", n)
}
m[n] = struct{}{}
}
Expand All @@ -128,7 +130,7 @@ func TestUint64(t *testing.T) {
for i := 0; i < 1e3; i++ {
n := Uint64()
if _, ok := m[n]; ok {
t.Fatalf("number %v already exists", n)
assert.Fail(t, "number %v already exists", n)
}
m[n] = struct{}{}
}
Expand All @@ -148,11 +150,11 @@ func TestBool(t *testing.T) {
}

func TestFloat32(t *testing.T) {
assert.NotZero(t, Float32())
assert.NotZero(t, Float32())
assert.GreaterOrEqual(t, Float32(), float32(0))
assert.Less(t, Float32(), float32(1))
}

func TestFloat64(t *testing.T) {
assert.NotZero(t, Float64())
assert.NotZero(t, Float64())
assert.GreaterOrEqual(t, Float64(), 0.0)
assert.Less(t, Float64(), 1.0)
}

0 comments on commit 3349083

Please sign in to comment.