summaryrefslogtreecommitdiff
path: root/src/pkg/rand
diff options
context:
space:
mode:
Diffstat (limited to 'src/pkg/rand')
-rw-r--r--src/pkg/rand/exp.go12
-rw-r--r--src/pkg/rand/normal.go20
-rw-r--r--src/pkg/rand/rand.go96
-rw-r--r--src/pkg/rand/rand_test.go202
-rw-r--r--src/pkg/rand/rng.go68
5 files changed, 199 insertions, 199 deletions
diff --git a/src/pkg/rand/exp.go b/src/pkg/rand/exp.go
index aaa3cc7b0..85da49521 100644
--- a/src/pkg/rand/exp.go
+++ b/src/pkg/rand/exp.go
@@ -5,7 +5,7 @@
package rand
import (
- "math";
+ "math"
)
/*
@@ -17,7 +17,7 @@ import (
*/
const (
- re = 7.69711747013104972;
+ re = 7.69711747013104972
)
// ExpFloat64 returns an exponentially distributed float64 in the range
@@ -30,9 +30,9 @@ const (
//
func (r *Rand) ExpFloat64() float64 {
for {
- j := r.Uint32();
- i := j & 0xFF;
- x := float64(j) * float64(we[i]);
+ j := r.Uint32()
+ i := j & 0xFF
+ x := float64(j) * float64(we[i])
if j < ke[i] {
return x
}
@@ -43,7 +43,7 @@ func (r *Rand) ExpFloat64() float64 {
return x
}
}
- panic("unreachable");
+ panic("unreachable")
}
var ke = [256]uint32{
diff --git a/src/pkg/rand/normal.go b/src/pkg/rand/normal.go
index 25769c7bf..9ab46db9f 100644
--- a/src/pkg/rand/normal.go
+++ b/src/pkg/rand/normal.go
@@ -5,7 +5,7 @@
package rand
import (
- "math";
+ "math"
)
/*
@@ -17,14 +17,14 @@ import (
*/
const (
- rn = 3.442619855899;
+ rn = 3.442619855899
)
func absInt32(i int32) uint32 {
if i < 0 {
return uint32(-i)
}
- return uint32(i);
+ return uint32(i)
}
// NormFloat64 returns a normally distributed float64 in the range
@@ -37,9 +37,9 @@ func absInt32(i int32) uint32 {
//
func (r *Rand) NormFloat64() float64 {
for {
- j := int32(r.Uint32()); // Possibly negative
- i := j & 0x7F;
- x := float64(j) * float64(wn[i]);
+ j := int32(r.Uint32()) // Possibly negative
+ i := j & 0x7F
+ x := float64(j) * float64(wn[i])
if absInt32(j) < kn[i] {
// This case should be hit better than 99% of the time.
return x
@@ -48,8 +48,8 @@ func (r *Rand) NormFloat64() float64 {
if i == 0 {
// This extra work is only required for the base strip.
for {
- x = -math.Log(r.Float64()) * (1.0 / rn);
- y := -math.Log(r.Float64());
+ x = -math.Log(r.Float64()) * (1.0 / rn)
+ y := -math.Log(r.Float64())
if y+y >= x*x {
break
}
@@ -57,13 +57,13 @@ func (r *Rand) NormFloat64() float64 {
if j > 0 {
return rn + x
}
- return -rn - x;
+ return -rn - x
}
if fn[i]+float32(r.Float64())*(fn[i-1]-fn[i]) < float32(math.Exp(-.5*x*x)) {
return x
}
}
- panic("unreachable");
+ panic("unreachable")
}
var kn = [128]uint32{
diff --git a/src/pkg/rand/rand.go b/src/pkg/rand/rand.go
index 0063e4059..0d7eaa79a 100644
--- a/src/pkg/rand/rand.go
+++ b/src/pkg/rand/rand.go
@@ -10,42 +10,42 @@ import "sync"
// A Source represents a source of uniformly-distributed
// pseudo-random int64 values in the range [0, 1<<63).
type Source interface {
- Int63() int64;
- Seed(seed int64);
+ Int63() int64
+ Seed(seed int64)
}
// NewSource returns a new pseudo-random Source seeded with the given value.
func NewSource(seed int64) Source {
- var rng rngSource;
- rng.Seed(seed);
- return &rng;
+ var rng rngSource
+ rng.Seed(seed)
+ return &rng
}
// A Rand is a source of random numbers.
type Rand struct {
- src Source;
+ src Source
}
// New returns a new Rand that uses random values from src
// to generate other random values.
-func New(src Source) *Rand { return &Rand{src} }
+func New(src Source) *Rand { return &Rand{src} }
// Seed uses the provided seed value to initialize the generator to a deterministic state.
-func (r *Rand) Seed(seed int64) { r.src.Seed(seed) }
+func (r *Rand) Seed(seed int64) { r.src.Seed(seed) }
// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
-func (r *Rand) Int63() int64 { return r.src.Int63() }
+func (r *Rand) Int63() int64 { return r.src.Int63() }
// Uint32 returns a pseudo-random 32-bit value as a uint32.
-func (r *Rand) Uint32() uint32 { return uint32(r.Int63() >> 31) }
+func (r *Rand) Uint32() uint32 { return uint32(r.Int63() >> 31) }
// Int31 returns a non-negative pseudo-random 31-bit integer as an int32.
-func (r *Rand) Int31() int32 { return int32(r.Int63() >> 32) }
+func (r *Rand) Int31() int32 { return int32(r.Int63() >> 32) }
// Int returns a non-negative pseudo-random int.
func (r *Rand) Int() int {
- u := uint(r.Int63());
- return int(u << 1 >> 1); // clear sign bit if int == int32
+ u := uint(r.Int63())
+ return int(u << 1 >> 1) // clear sign bit if int == int32
}
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
@@ -53,40 +53,40 @@ func (r *Rand) Int63n(n int64) int64 {
if n <= 0 {
return 0
}
- max := int64((1 << 63) - 1 - (1<<63)%uint64(n));
- v := r.Int63();
+ max := int64((1 << 63) - 1 - (1<<63)%uint64(n))
+ v := r.Int63()
for v > max {
v = r.Int63()
}
- return v % n;
+ return v % n
}
// Int31n returns, as an int32, a non-negative pseudo-random number in [0,n).
-func (r *Rand) Int31n(n int32) int32 { return int32(r.Int63n(int64(n))) }
+func (r *Rand) Int31n(n int32) int32 { return int32(r.Int63n(int64(n))) }
// Intn returns, as an int, a non-negative pseudo-random number in [0,n).
-func (r *Rand) Intn(n int) int { return int(r.Int63n(int64(n))) }
+func (r *Rand) Intn(n int) int { return int(r.Int63n(int64(n))) }
// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
-func (r *Rand) Float64() float64 { return float64(r.Int63()) / (1 << 63) }
+func (r *Rand) Float64() float64 { return float64(r.Int63()) / (1 << 63) }
// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
-func (r *Rand) Float32() float32 { return float32(r.Float64()) }
+func (r *Rand) Float32() float32 { return float32(r.Float64()) }
// Float returns, as a float, a pseudo-random number in [0.0,1.0).
-func (r *Rand) Float() float { return float(r.Float64()) }
+func (r *Rand) Float() float { return float(r.Float64()) }
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
func (r *Rand) Perm(n int) []int {
- m := make([]int, n);
+ m := make([]int, n)
for i := 0; i < n; i++ {
m[i] = i
}
for i := 0; i < n; i++ {
- j := r.Intn(i + 1);
- m[i], m[j] = m[j], m[i];
+ j := r.Intn(i + 1)
+ m[i], m[j] = m[j], m[i]
}
- return m;
+ return m
}
/*
@@ -96,40 +96,40 @@ func (r *Rand) Perm(n int) []int {
var globalRand = New(&lockedSource{src: NewSource(1)})
// Seed uses the provided seed value to initialize the generator to a deterministic state.
-func Seed(seed int64) { globalRand.Seed(seed) }
+func Seed(seed int64) { globalRand.Seed(seed) }
// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
-func Int63() int64 { return globalRand.Int63() }
+func Int63() int64 { return globalRand.Int63() }
// Uint32 returns a pseudo-random 32-bit value as a uint32.
-func Uint32() uint32 { return globalRand.Uint32() }
+func Uint32() uint32 { return globalRand.Uint32() }
// Int31 returns a non-negative pseudo-random 31-bit integer as an int32.
-func Int31() int32 { return globalRand.Int31() }
+func Int31() int32 { return globalRand.Int31() }
// Int returns a non-negative pseudo-random int.
-func Int() int { return globalRand.Int() }
+func Int() int { return globalRand.Int() }
// Int63n returns, as an int64, a non-negative pseudo-random number in [0,n).
-func Int63n(n int64) int64 { return globalRand.Int63n(n) }
+func Int63n(n int64) int64 { return globalRand.Int63n(n) }
// Int31n returns, as an int32, a non-negative pseudo-random number in [0,n).
-func Int31n(n int32) int32 { return globalRand.Int31n(n) }
+func Int31n(n int32) int32 { return globalRand.Int31n(n) }
// Intn returns, as an int, a non-negative pseudo-random number in [0,n).
-func Intn(n int) int { return globalRand.Intn(n) }
+func Intn(n int) int { return globalRand.Intn(n) }
// Float64 returns, as a float64, a pseudo-random number in [0.0,1.0).
-func Float64() float64 { return globalRand.Float64() }
+func Float64() float64 { return globalRand.Float64() }
// Float32 returns, as a float32, a pseudo-random number in [0.0,1.0).
-func Float32() float32 { return globalRand.Float32() }
+func Float32() float32 { return globalRand.Float32() }
// Float returns, as a float, a pseudo-random number in [0.0,1.0).
-func Float() float { return globalRand.Float() }
+func Float() float { return globalRand.Float() }
// Perm returns, as a slice of n ints, a pseudo-random permutation of the integers [0,n).
-func Perm(n int) []int { return globalRand.Perm(n) }
+func Perm(n int) []int { return globalRand.Perm(n) }
// NormFloat64 returns a normally distributed float64 in the range
// [-math.MaxFloat64, +math.MaxFloat64] with
@@ -139,7 +139,7 @@ func Perm(n int) []int { return globalRand.Perm(n) }
//
// sample = NormFloat64() * desiredStdDev + desiredMean
//
-func NormFloat64() float64 { return globalRand.NormFloat64() }
+func NormFloat64() float64 { return globalRand.NormFloat64() }
// ExpFloat64 returns an exponentially distributed float64 in the range
// (0, +math.MaxFloat64] with an exponential distribution whose rate parameter
@@ -149,22 +149,22 @@ func NormFloat64() float64 { return globalRand.NormFloat64() }
//
// sample = ExpFloat64() / desiredRateParameter
//
-func ExpFloat64() float64 { return globalRand.ExpFloat64() }
+func ExpFloat64() float64 { return globalRand.ExpFloat64() }
type lockedSource struct {
- lk sync.Mutex;
- src Source;
+ lk sync.Mutex
+ src Source
}
func (r *lockedSource) Int63() (n int64) {
- r.lk.Lock();
- n = r.src.Int63();
- r.lk.Unlock();
- return;
+ r.lk.Lock()
+ n = r.src.Int63()
+ r.lk.Unlock()
+ return
}
func (r *lockedSource) Seed(seed int64) {
- r.lk.Lock();
- r.src.Seed(seed);
- r.lk.Unlock();
+ r.lk.Lock()
+ r.src.Seed(seed)
+ r.lk.Unlock()
}
diff --git a/src/pkg/rand/rand_test.go b/src/pkg/rand/rand_test.go
index e0d1da4bb..b90c69db7 100644
--- a/src/pkg/rand/rand_test.go
+++ b/src/pkg/rand/rand_test.go
@@ -5,36 +5,36 @@
package rand
import (
- "math";
- "fmt";
- "os";
- "testing";
+ "math"
+ "fmt"
+ "os"
+ "testing"
)
const (
- numTestSamples = 10000;
+ numTestSamples = 10000
)
type statsResults struct {
- mean float64;
- stddev float64;
- closeEnough float64;
- maxError float64;
+ mean float64
+ stddev float64
+ closeEnough float64
+ maxError float64
}
func max(a, b float64) float64 {
if a > b {
return a
}
- return b;
+ return b
}
func nearEqual(a, b, closeEnough, maxError float64) bool {
- absDiff := math.Fabs(a - b);
- if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
+ absDiff := math.Fabs(a - b)
+ if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
return true
}
- return absDiff/max(math.Fabs(a), math.Fabs(b)) < maxError;
+ return absDiff/max(math.Fabs(a), math.Fabs(b)) < maxError
}
var testSeeds = []int64{1, 1754801282, 1698661970, 1550503961}
@@ -43,52 +43,52 @@ var testSeeds = []int64{1, 1754801282, 1698661970, 1550503961}
// two statsResults are similar.
func (this *statsResults) checkSimilarDistribution(expected *statsResults) os.Error {
if !nearEqual(this.mean, expected.mean, expected.closeEnough, expected.maxError) {
- s := fmt.Sprintf("mean %v != %v (allowed error %v, %v)", this.mean, expected.mean, expected.closeEnough, expected.maxError);
- fmt.Println(s);
- return os.ErrorString(s);
+ s := fmt.Sprintf("mean %v != %v (allowed error %v, %v)", this.mean, expected.mean, expected.closeEnough, expected.maxError)
+ fmt.Println(s)
+ return os.ErrorString(s)
}
if !nearEqual(this.stddev, expected.stddev, 0, expected.maxError) {
- s := fmt.Sprintf("stddev %v != %v (allowed error %v, %v)", this.stddev, expected.stddev, expected.closeEnough, expected.maxError);
- fmt.Println(s);
- return os.ErrorString(s);
+ s := fmt.Sprintf("stddev %v != %v (allowed error %v, %v)", this.stddev, expected.stddev, expected.closeEnough, expected.maxError)
+ fmt.Println(s)
+ return os.ErrorString(s)
}
- return nil;
+ return nil
}
func getStatsResults(samples []float64) *statsResults {
- res := new(statsResults);
- var sum float64;
+ res := new(statsResults)
+ var sum float64
for i := range samples {
sum += samples[i]
}
- res.mean = sum / float64(len(samples));
- var devsum float64;
+ res.mean = sum / float64(len(samples))
+ var devsum float64
for i := range samples {
devsum += math.Pow(samples[i]-res.mean, 2)
}
- res.stddev = math.Sqrt(devsum / float64(len(samples)));
- return res;
+ res.stddev = math.Sqrt(devsum / float64(len(samples)))
+ return res
}
func checkSampleDistribution(t *testing.T, samples []float64, expected *statsResults) {
- actual := getStatsResults(samples);
- err := actual.checkSimilarDistribution(expected);
+ actual := getStatsResults(samples)
+ err := actual.checkSimilarDistribution(expected)
if err != nil {
t.Errorf(err.String())
}
}
func checkSampleSliceDistributions(t *testing.T, samples []float64, nslices int, expected *statsResults) {
- chunk := len(samples) / nslices;
+ chunk := len(samples) / nslices
for i := 0; i < nslices; i++ {
- low := i * chunk;
- var high int;
+ low := i * chunk
+ var high int
if i == nslices-1 {
high = len(samples) - 1
} else {
high = (i + 1) * chunk
}
- checkSampleDistribution(t, samples[low:high], expected);
+ checkSampleDistribution(t, samples[low:high], expected)
}
}
@@ -97,29 +97,29 @@ func checkSampleSliceDistributions(t *testing.T, samples []float64, nslices int,
//
func generateNormalSamples(nsamples int, mean, stddev float64, seed int64) []float64 {
- r := New(NewSource(seed));
- samples := make([]float64, nsamples);
+ r := New(NewSource(seed))
+ samples := make([]float64, nsamples)
for i := range samples {
samples[i] = r.NormFloat64()*stddev + mean
}
- return samples;
+ return samples
}
func testNormalDistribution(t *testing.T, nsamples int, mean, stddev float64, seed int64) {
//fmt.Printf("testing nsamples=%v mean=%v stddev=%v seed=%v\n", nsamples, mean, stddev, seed);
- samples := generateNormalSamples(nsamples, mean, stddev, seed);
- errorScale := max(1.0, stddev); // Error scales with stddev
- expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale};
+ samples := generateNormalSamples(nsamples, mean, stddev, seed)
+ errorScale := max(1.0, stddev) // Error scales with stddev
+ expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale}
// Make sure that the entire set matches the expected distribution.
- checkSampleDistribution(t, samples, expected);
+ checkSampleDistribution(t, samples, expected)
// Make sure that each half of the set matches the expected distribution.
- checkSampleSliceDistributions(t, samples, 2, expected);
+ checkSampleSliceDistributions(t, samples, 2, expected)
// Make sure that each 7th of the set matches the expected distribution.
- checkSampleSliceDistributions(t, samples, 7, expected);
+ checkSampleSliceDistributions(t, samples, 7, expected)
}
// Actual tests
@@ -145,32 +145,32 @@ func TestNonStandardNormalValues(t *testing.T) {
//
func generateExponentialSamples(nsamples int, rate float64, seed int64) []float64 {
- r := New(NewSource(seed));
- samples := make([]float64, nsamples);
+ r := New(NewSource(seed))
+ samples := make([]float64, nsamples)
for i := range samples {
samples[i] = r.ExpFloat64() / rate
}
- return samples;
+ return samples
}
func testExponentialDistribution(t *testing.T, nsamples int, rate float64, seed int64) {
//fmt.Printf("testing nsamples=%v rate=%v seed=%v\n", nsamples, rate, seed);
- mean := 1 / rate;
- stddev := mean;
+ mean := 1 / rate
+ stddev := mean
- samples := generateExponentialSamples(nsamples, rate, seed);
- errorScale := max(1.0, 1/rate); // Error scales with the inverse of the rate
- expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.20 * errorScale};
+ samples := generateExponentialSamples(nsamples, rate, seed)
+ errorScale := max(1.0, 1/rate) // Error scales with the inverse of the rate
+ expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.20 * errorScale}
// Make sure that the entire set matches the expected distribution.
- checkSampleDistribution(t, samples, expected);
+ checkSampleDistribution(t, samples, expected)
// Make sure that each half of the set matches the expected distribution.
- checkSampleSliceDistributions(t, samples, 2, expected);
+ checkSampleSliceDistributions(t, samples, 2, expected)
// Make sure that each 7th of the set matches the expected distribution.
- checkSampleSliceDistributions(t, samples, 7, expected);
+ checkSampleSliceDistributions(t, samples, 7, expected)
}
// Actual tests
@@ -194,61 +194,61 @@ func TestNonStandardExponentialValues(t *testing.T) {
//
func initNorm() (testKn []uint32, testWn, testFn []float32) {
- const m1 = 1 << 31;
+ const m1 = 1 << 31
var (
- dn float64 = rn;
- tn = dn;
- vn float64 = 9.91256303526217e-3;
+ dn float64 = rn
+ tn = dn
+ vn float64 = 9.91256303526217e-3
)
- testKn = make([]uint32, 128);
- testWn = make([]float32, 128);
- testFn = make([]float32, 128);
-
- q := vn / math.Exp(-0.5*dn*dn);
- testKn[0] = uint32((dn / q) * m1);
- testKn[1] = 0;
- testWn[0] = float32(q / m1);
- testWn[127] = float32(dn / m1);
- testFn[0] = 1.0;
- testFn[127] = float32(math.Exp(-0.5 * dn * dn));
+ testKn = make([]uint32, 128)
+ testWn = make([]float32, 128)
+ testFn = make([]float32, 128)
+
+ q := vn / math.Exp(-0.5*dn*dn)
+ testKn[0] = uint32((dn / q) * m1)
+ testKn[1] = 0
+ testWn[0] = float32(q / m1)
+ testWn[127] = float32(dn / m1)
+ testFn[0] = 1.0
+ testFn[127] = float32(math.Exp(-0.5 * dn * dn))
for i := 126; i >= 1; i-- {
- dn = math.Sqrt(-2.0 * math.Log(vn/dn+math.Exp(-0.5*dn*dn)));
- testKn[i+1] = uint32((dn / tn) * m1);
- tn = dn;
- testFn[i] = float32(math.Exp(-0.5 * dn * dn));
- testWn[i] = float32(dn / m1);
+ dn = math.Sqrt(-2.0 * math.Log(vn/dn+math.Exp(-0.5*dn*dn)))
+ testKn[i+1] = uint32((dn / tn) * m1)
+ tn = dn
+ testFn[i] = float32(math.Exp(-0.5 * dn * dn))
+ testWn[i] = float32(dn / m1)
}
- return;
+ return
}
func initExp() (testKe []uint32, testWe, testFe []float32) {
- const m2 = 1 << 32;
+ const m2 = 1 << 32
var (
- de float64 = re;
- te = de;
- ve float64 = 3.9496598225815571993e-3;
+ de float64 = re
+ te = de
+ ve float64 = 3.9496598225815571993e-3
)
- testKe = make([]uint32, 256);
- testWe = make([]float32, 256);
- testFe = make([]float32, 256);
-
- q := ve / math.Exp(-de);
- testKe[0] = uint32((de / q) * m2);
- testKe[1] = 0;
- testWe[0] = float32(q / m2);
- testWe[255] = float32(de / m2);
- testFe[0] = 1.0;
- testFe[255] = float32(math.Exp(-de));
+ testKe = make([]uint32, 256)
+ testWe = make([]float32, 256)
+ testFe = make([]float32, 256)
+
+ q := ve / math.Exp(-de)
+ testKe[0] = uint32((de / q) * m2)
+ testKe[1] = 0
+ testWe[0] = float32(q / m2)
+ testWe[255] = float32(de / m2)
+ testFe[0] = 1.0
+ testFe[255] = float32(math.Exp(-de))
for i := 254; i >= 1; i-- {
- de = -math.Log(ve/de + math.Exp(-de));
- testKe[i+1] = uint32((de / te) * m2);
- te = de;
- testFe[i] = float32(math.Exp(-de));
- testWe[i] = float32(de / m2);
+ de = -math.Log(ve/de + math.Exp(-de))
+ testKe[i+1] = uint32((de / te) * m2)
+ te = de
+ testFe[i] = float32(math.Exp(-de))
+ testWe[i] = float32(de / m2)
}
- return;
+ return
}
// compareUint32Slices returns the first index where the two slices
@@ -259,14 +259,14 @@ func compareUint32Slices(s1, s2 []uint32) int {
if len(s1) > len(s2) {
return len(s2) + 1
}
- return len(s1) + 1;
+ return len(s1) + 1
}
for i := range s1 {
if s1[i] != s2[i] {
return i
}
}
- return -1;
+ return -1
}
// compareFloat32Slices returns the first index where the two slices
@@ -277,18 +277,18 @@ func compareFloat32Slices(s1, s2 []float32) int {
if len(s1) > len(s2) {
return len(s2) + 1
}
- return len(s1) + 1;
+ return len(s1) + 1
}
for i := range s1 {
if !nearEqual(float64(s1[i]), float64(s2[i]), 0, 1e-7) {
return i
}
}
- return -1;
+ return -1
}
func TestNormTables(t *testing.T) {
- testKn, testWn, testFn := initNorm();
+ testKn, testWn, testFn := initNorm()
if i := compareUint32Slices(kn[0:], testKn); i >= 0 {
t.Errorf("kn disagrees at index %v; %v != %v\n", i, kn[i], testKn[i])
}
@@ -301,7 +301,7 @@ func TestNormTables(t *testing.T) {
}
func TestExpTables(t *testing.T) {
- testKe, testWe, testFe := initExp();
+ testKe, testWe, testFe := initExp()
if i := compareUint32Slices(ke[0:], testKe); i >= 0 {
t.Errorf("ke disagrees at index %v; %v != %v\n", i, ke[i], testKe[i])
}
@@ -322,7 +322,7 @@ func BenchmarkInt63Threadsafe(b *testing.B) {
}
func BenchmarkInt63Unthreadsafe(b *testing.B) {
- r := New(NewSource(1));
+ r := New(NewSource(1))
for n := b.N; n > 0; n-- {
r.Int63()
}
diff --git a/src/pkg/rand/rng.go b/src/pkg/rand/rng.go
index a8f7a18b1..947c49f0f 100644
--- a/src/pkg/rand/rng.go
+++ b/src/pkg/rand/rng.go
@@ -12,14 +12,14 @@ package rand
*/
const (
- _LEN = 607;
- _TAP = 273;
- _MAX = 1 << 63;
- _MASK = _MAX - 1;
- _A = 48271;
- _M = (1 << 31) - 1;
- _Q = 44488;
- _R = 3399;
+ _LEN = 607
+ _TAP = 273
+ _MAX = 1 << 63
+ _MASK = _MAX - 1
+ _A = 48271
+ _M = (1 << 31) - 1
+ _Q = 44488
+ _R = 3399
)
var (
@@ -179,32 +179,32 @@ var (
4922828954023452664, 2879211533496425641, 5896236396443472108, 8465043815351752425,
7329020396871624740, 8915471717014488588, 2944902635677463047, 7052079073493465134,
8382142935188824023, 9103922860780351547, 4152330101494654406,
- };
+ }
)
type rngSource struct {
- tap int; // index into vec
- feed int; // index into vec
- vec [_LEN]int64; // current feedback register
+ tap int // index into vec
+ feed int // index into vec
+ vec [_LEN]int64 // current feedback register
}
// seed rng x[n+1] = 48271 * x[n] mod (2**31 - 1)
func seedrand(x int32) int32 {
- hi := x / _Q;
- lo := x % _Q;
- x = _A*lo - _R*hi;
+ hi := x / _Q
+ lo := x % _Q
+ x = _A*lo - _R*hi
if x < 0 {
x += _M
}
- return x;
+ return x
}
// Seed uses the provided seed value to initialize the generator to a deterministic state.
func (rng *rngSource) Seed(seed int64) {
- rng.tap = 0;
- rng.feed = _LEN - _TAP;
+ rng.tap = 0
+ rng.feed = _LEN - _TAP
- seed = seed % _M;
+ seed = seed % _M
if seed < 0 {
seed += _M
}
@@ -212,35 +212,35 @@ func (rng *rngSource) Seed(seed int64) {
seed = 89482311
}
- x := int32(seed);
+ x := int32(seed)
for i := -20; i < _LEN; i++ {
- x = seedrand(x);
+ x = seedrand(x)
if i >= 0 {
- var u int64;
- u = int64(x) << 40;
- x = seedrand(x);
- u ^= int64(x) << 20;
- x = seedrand(x);
- u ^= int64(x);
- u ^= rng_cooked[i];
- rng.vec[i] = u & _MASK;
+ var u int64
+ u = int64(x) << 40
+ x = seedrand(x)
+ u ^= int64(x) << 20
+ x = seedrand(x)
+ u ^= int64(x)
+ u ^= rng_cooked[i]
+ rng.vec[i] = u & _MASK
}
}
}
// Int63 returns a non-negative pseudo-random 63-bit integer as an int64.
func (rng *rngSource) Int63() int64 {
- rng.tap--;
+ rng.tap--
if rng.tap < 0 {
rng.tap += _LEN
}
- rng.feed--;
+ rng.feed--
if rng.feed < 0 {
rng.feed += _LEN
}
- x := (rng.vec[rng.feed] + rng.vec[rng.tap]) & _MASK;
- rng.vec[rng.feed] = x;
- return x;
+ x := (rng.vec[rng.feed] + rng.vec[rng.tap]) & _MASK
+ rng.vec[rng.feed] = x
+ return x
}