1 // Copyright 2009 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
23 numTestSamples = 10000
26 var rn, kn, wn, fn = GetNormalDistributionParameters()
27 var re, ke, we, fe = GetExponentialDistributionParameters()
29 type statsResults struct {
36 func max(a, b float64) float64 {
43 func nearEqual(a, b, closeEnough, maxError float64) bool {
44 absDiff := math.Abs(a - b)
45 if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
48 return absDiff/max(math.Abs(a), math.Abs(b)) < maxError
51 var testSeeds = []int64{1, 1754801282, 1698661970, 1550503961}
53 // checkSimilarDistribution returns success if the mean and stddev of the
54 // two statsResults are similar.
55 func (this *statsResults) checkSimilarDistribution(expected *statsResults) error {
56 if !nearEqual(this.mean, expected.mean, expected.closeEnough, expected.maxError) {
57 s := fmt.Sprintf("mean %v != %v (allowed error %v, %v)", this.mean, expected.mean, expected.closeEnough, expected.maxError)
61 if !nearEqual(this.stddev, expected.stddev, expected.closeEnough, expected.maxError) {
62 s := fmt.Sprintf("stddev %v != %v (allowed error %v, %v)", this.stddev, expected.stddev, expected.closeEnough, expected.maxError)
69 func getStatsResults(samples []float64) *statsResults {
70 res := new(statsResults)
71 var sum, squaresum float64
72 for _, s := range samples {
76 res.mean = sum / float64(len(samples))
77 res.stddev = math.Sqrt(squaresum/float64(len(samples)) - res.mean*res.mean)
81 func checkSampleDistribution(t *testing.T, samples []float64, expected *statsResults) {
83 actual := getStatsResults(samples)
84 err := actual.checkSimilarDistribution(expected)
90 func checkSampleSliceDistributions(t *testing.T, samples []float64, nslices int, expected *statsResults) {
92 chunk := len(samples) / nslices
93 for i := 0; i < nslices; i++ {
97 high = len(samples) - 1
99 high = (i + 1) * chunk
101 checkSampleDistribution(t, samples[low:high], expected)
106 // Normal distribution tests
109 func generateNormalSamples(nsamples int, mean, stddev float64, seed int64) []float64 {
110 r := New(NewSource(seed))
111 samples := make([]float64, nsamples)
112 for i := range samples {
113 samples[i] = r.NormFloat64()*stddev + mean
118 func testNormalDistribution(t *testing.T, nsamples int, mean, stddev float64, seed int64) {
119 //fmt.Printf("testing nsamples=%v mean=%v stddev=%v seed=%v\n", nsamples, mean, stddev, seed);
121 samples := generateNormalSamples(nsamples, mean, stddev, seed)
122 errorScale := max(1.0, stddev) // Error scales with stddev
123 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale}
125 // Make sure that the entire set matches the expected distribution.
126 checkSampleDistribution(t, samples, expected)
128 // Make sure that each half of the set matches the expected distribution.
129 checkSampleSliceDistributions(t, samples, 2, expected)
131 // Make sure that each 7th of the set matches the expected distribution.
132 checkSampleSliceDistributions(t, samples, 7, expected)
137 func TestStandardNormalValues(t *testing.T) {
138 for _, seed := range testSeeds {
139 testNormalDistribution(t, numTestSamples, 0, 1, seed)
143 func TestNonStandardNormalValues(t *testing.T) {
150 for sd := 0.5; sd < sdmax; sd *= 2 {
151 for m := 0.5; m < mmax; m *= 2 {
152 for _, seed := range testSeeds {
153 testNormalDistribution(t, numTestSamples, m, sd, seed)
163 // Exponential distribution tests
166 func generateExponentialSamples(nsamples int, rate float64, seed int64) []float64 {
167 r := New(NewSource(seed))
168 samples := make([]float64, nsamples)
169 for i := range samples {
170 samples[i] = r.ExpFloat64() / rate
175 func testExponentialDistribution(t *testing.T, nsamples int, rate float64, seed int64) {
176 //fmt.Printf("testing nsamples=%v rate=%v seed=%v\n", nsamples, rate, seed);
181 samples := generateExponentialSamples(nsamples, rate, seed)
182 errorScale := max(1.0, 1/rate) // Error scales with the inverse of the rate
183 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.20 * errorScale}
185 // Make sure that the entire set matches the expected distribution.
186 checkSampleDistribution(t, samples, expected)
188 // Make sure that each half of the set matches the expected distribution.
189 checkSampleSliceDistributions(t, samples, 2, expected)
191 // Make sure that each 7th of the set matches the expected distribution.
192 checkSampleSliceDistributions(t, samples, 7, expected)
197 func TestStandardExponentialValues(t *testing.T) {
198 for _, seed := range testSeeds {
199 testExponentialDistribution(t, numTestSamples, 1, seed)
203 func TestNonStandardExponentialValues(t *testing.T) {
204 for rate := 0.05; rate < 10; rate *= 2 {
205 for _, seed := range testSeeds {
206 testExponentialDistribution(t, numTestSamples, rate, seed)
215 // Table generation tests
218 func initNorm() (testKn []uint32, testWn, testFn []float32) {
223 vn float64 = 9.91256303526217e-3
226 testKn = make([]uint32, 128)
227 testWn = make([]float32, 128)
228 testFn = make([]float32, 128)
230 q := vn / math.Exp(-0.5*dn*dn)
231 testKn[0] = uint32((dn / q) * m1)
233 testWn[0] = float32(q / m1)
234 testWn[127] = float32(dn / m1)
236 testFn[127] = float32(math.Exp(-0.5 * dn * dn))
237 for i := 126; i >= 1; i-- {
238 dn = math.Sqrt(-2.0 * math.Log(vn/dn+math.Exp(-0.5*dn*dn)))
239 testKn[i+1] = uint32((dn / tn) * m1)
241 testFn[i] = float32(math.Exp(-0.5 * dn * dn))
242 testWn[i] = float32(dn / m1)
247 func initExp() (testKe []uint32, testWe, testFe []float32) {
252 ve float64 = 3.9496598225815571993e-3
255 testKe = make([]uint32, 256)
256 testWe = make([]float32, 256)
257 testFe = make([]float32, 256)
259 q := ve / math.Exp(-de)
260 testKe[0] = uint32((de / q) * m2)
262 testWe[0] = float32(q / m2)
263 testWe[255] = float32(de / m2)
265 testFe[255] = float32(math.Exp(-de))
266 for i := 254; i >= 1; i-- {
267 de = -math.Log(ve/de + math.Exp(-de))
268 testKe[i+1] = uint32((de / te) * m2)
270 testFe[i] = float32(math.Exp(-de))
271 testWe[i] = float32(de / m2)
276 // compareUint32Slices returns the first index where the two slices
277 // disagree, or <0 if the lengths are the same and all elements
279 func compareUint32Slices(s1, s2 []uint32) int {
280 if len(s1) != len(s2) {
281 if len(s1) > len(s2) {
294 // compareFloat32Slices returns the first index where the two slices
295 // disagree, or <0 if the lengths are the same and all elements
297 func compareFloat32Slices(s1, s2 []float32) int {
298 if len(s1) != len(s2) {
299 if len(s1) > len(s2) {
305 if !nearEqual(float64(s1[i]), float64(s2[i]), 0, 1e-7) {
312 func TestNormTables(t *testing.T) {
313 testKn, testWn, testFn := initNorm()
314 if i := compareUint32Slices(kn[0:], testKn); i >= 0 {
315 t.Errorf("kn disagrees at index %v; %v != %v", i, kn[i], testKn[i])
317 if i := compareFloat32Slices(wn[0:], testWn); i >= 0 {
318 t.Errorf("wn disagrees at index %v; %v != %v", i, wn[i], testWn[i])
320 if i := compareFloat32Slices(fn[0:], testFn); i >= 0 {
321 t.Errorf("fn disagrees at index %v; %v != %v", i, fn[i], testFn[i])
325 func TestExpTables(t *testing.T) {
326 testKe, testWe, testFe := initExp()
327 if i := compareUint32Slices(ke[0:], testKe); i >= 0 {
328 t.Errorf("ke disagrees at index %v; %v != %v", i, ke[i], testKe[i])
330 if i := compareFloat32Slices(we[0:], testWe); i >= 0 {
331 t.Errorf("we disagrees at index %v; %v != %v", i, we[i], testWe[i])
333 if i := compareFloat32Slices(fe[0:], testFe); i >= 0 {
334 t.Errorf("fe disagrees at index %v; %v != %v", i, fe[i], testFe[i])
338 func hasSlowFloatingPoint() bool {
339 switch runtime.GOARCH {
341 return os.Getenv("GOARM") == "5"
342 case "mips", "mipsle", "mips64", "mips64le":
343 // Be conservative and assume that all mips boards
344 // have emulated floating point.
345 // TODO: detect what it actually has.
351 func TestFloat32(t *testing.T) {
352 // For issue 6721, the problem came after 7533753 calls, so check 10e6.
354 // But do the full amount only on builders (not locally).
355 // But ARM5 floating point emulation is slow (Issue 10749), so
356 // do less for that builder:
357 if testing.Short() && (testenv.Builder() == "" || hasSlowFloatingPoint()) {
358 num /= 100 // 1.72 seconds instead of 172 seconds
361 r := New(NewSource(1))
362 for ct := 0; ct < num; ct++ {
365 t.Fatal("Float32() should be in range [0,1). ct:", ct, "f:", f)
370 func testReadUniformity(t *testing.T, n int, seed int64) {
371 r := New(NewSource(seed))
372 buf := make([]byte, n)
373 nRead, err := r.Read(buf)
375 t.Errorf("Read err %v", err)
378 t.Errorf("Read returned unexpected n; %d != %d", nRead, n)
381 // Expect a uniform distribution of byte values, which lie in [0, 255].
384 stddev = 256.0 / math.Sqrt(12.0)
385 errorScale = stddev / math.Sqrt(float64(n))
388 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale}
390 // Cast bytes as floats to use the common distribution-validity checks.
391 samples := make([]float64, n)
392 for i, val := range buf {
393 samples[i] = float64(val)
395 // Make sure that the entire set matches the expected distribution.
396 checkSampleDistribution(t, samples, expected)
399 func TestReadUniformity(t *testing.T) {
400 testBufferSizes := []int{
401 2, 4, 7, 64, 1024, 1 << 16, 1 << 20,
403 for _, seed := range testSeeds {
404 for _, n := range testBufferSizes {
405 testReadUniformity(t, n, seed)
410 func TestReadEmpty(t *testing.T) {
411 r := New(NewSource(1))
412 buf := make([]byte, 0)
413 n, err := r.Read(buf)
415 t.Errorf("Read err into empty buffer; %v", err)
418 t.Errorf("Read into empty buffer returned unexpected n of %d", n)
422 func TestReadByOneByte(t *testing.T) {
423 r := New(NewSource(1))
424 b1 := make([]byte, 100)
425 _, err := io.ReadFull(iotest.OneByteReader(r), b1)
427 t.Errorf("read by one byte: %v", err)
429 r = New(NewSource(1))
430 b2 := make([]byte, 100)
433 t.Errorf("read: %v", err)
435 if !bytes.Equal(b1, b2) {
436 t.Errorf("read by one byte vs single read:\n%x\n%x", b1, b2)
440 func TestReadSeedReset(t *testing.T) {
441 r := New(NewSource(42))
442 b1 := make([]byte, 128)
445 t.Errorf("read: %v", err)
448 b2 := make([]byte, 128)
451 t.Errorf("read: %v", err)
453 if !bytes.Equal(b1, b2) {
454 t.Errorf("mismatch after re-seed:\n%x\n%x", b1, b2)
458 func TestShuffleSmall(t *testing.T) {
459 // Check that Shuffle allows n=0 and n=1, but that swap is never called for them.
460 r := New(NewSource(1))
461 for n := 0; n <= 1; n++ {
462 r.Shuffle(n, func(i, j int) { t.Fatalf("swap called, n=%d i=%d j=%d", n, i, j) })
466 // encodePerm converts from a permuted slice of length n, such as Perm generates, to an int in [0, n!).
467 // See https://en.wikipedia.org/wiki/Lehmer_code.
468 // encodePerm modifies the input slice.
469 func encodePerm(s []int) int {
470 // Convert to Lehmer code.
471 for i, x := range s {
473 for j, y := range r {
479 // Convert to int in [0, n!).
482 for i := len(s) - 1; i >= 0; i-- {
489 // TestUniformFactorial tests several ways of generating a uniform value in [0, n!).
490 func TestUniformFactorial(t *testing.T) {
491 r := New(NewSource(testSeeds[0]))
496 for n := 3; n <= top; n++ {
497 t.Run(fmt.Sprintf("n=%d", n), func(t *testing.T) {
500 for i := 2; i <= n; i++ {
504 // Test a few different ways to generate a uniform distribution.
505 p := make([]int, n) // re-usable slice for Shuffle generator
506 tests := [...]struct {
510 {name: "Int31n", fn: func() int { return int(r.Int31n(int32(nfact))) }},
511 {name: "int31n", fn: func() int { return int(Int31nForTest(r, int32(nfact))) }},
512 {name: "Perm", fn: func() int { return encodePerm(r.Perm(n)) }},
513 {name: "Shuffle", fn: func() int {
514 // Generate permutation using Shuffle.
518 r.Shuffle(n, func(i, j int) { p[i], p[j] = p[j], p[i] })
523 for _, test := range tests {
524 t.Run(test.name, func(t *testing.T) {
525 // Gather chi-squared values and check that they follow
526 // the expected normal distribution given n!-1 degrees of freedom.
527 // See https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test and
528 // https://www.johndcook.com/Beautiful_Testing_ch10.pdf.
529 nsamples := 10 * nfact
533 samples := make([]float64, nsamples)
534 for i := range samples {
535 // Generate some uniformly distributed values and count their occurrences.
537 counts := make([]int, nfact)
538 for i := 0; i < iters; i++ {
541 // Calculate chi-squared and add to samples.
542 want := iters / float64(nfact)
544 for _, have := range counts {
545 err := float64(have) - want
552 // Check that our samples approximate the appropriate normal distribution.
553 dof := float64(nfact - 1)
554 expected := &statsResults{mean: dof, stddev: math.Sqrt(2 * dof)}
555 errorScale := max(1.0, expected.stddev)
556 expected.closeEnough = 0.10 * errorScale
557 expected.maxError = 0.08 // TODO: What is the right value here? See issue 21211.
558 checkSampleDistribution(t, samples, expected)
567 func BenchmarkInt63Threadsafe(b *testing.B) {
568 for n := b.N; n > 0; n-- {
573 func BenchmarkInt63ThreadsafeParallel(b *testing.B) {
574 b.RunParallel(func(pb *testing.PB) {
581 func BenchmarkInt63Unthreadsafe(b *testing.B) {
582 r := New(NewSource(1))
583 for n := b.N; n > 0; n-- {
588 func BenchmarkIntn1000(b *testing.B) {
589 r := New(NewSource(1))
590 for n := b.N; n > 0; n-- {
595 func BenchmarkInt63n1000(b *testing.B) {
596 r := New(NewSource(1))
597 for n := b.N; n > 0; n-- {
602 func BenchmarkInt31n1000(b *testing.B) {
603 r := New(NewSource(1))
604 for n := b.N; n > 0; n-- {
609 func BenchmarkFloat32(b *testing.B) {
610 r := New(NewSource(1))
611 for n := b.N; n > 0; n-- {
616 func BenchmarkFloat64(b *testing.B) {
617 r := New(NewSource(1))
618 for n := b.N; n > 0; n-- {
623 func BenchmarkPerm3(b *testing.B) {
624 r := New(NewSource(1))
625 for n := b.N; n > 0; n-- {
630 func BenchmarkPerm30(b *testing.B) {
631 r := New(NewSource(1))
632 for n := b.N; n > 0; n-- {
637 func BenchmarkPerm30ViaShuffle(b *testing.B) {
638 r := New(NewSource(1))
639 for n := b.N; n > 0; n-- {
644 r.Shuffle(30, func(i, j int) { p[i], p[j] = p[j], p[i] })
648 // BenchmarkShuffleOverhead uses a minimal swap function
649 // to measure just the shuffling overhead.
650 func BenchmarkShuffleOverhead(b *testing.B) {
651 r := New(NewSource(1))
652 for n := b.N; n > 0; n-- {
653 r.Shuffle(52, func(i, j int) {
654 if i < 0 || i >= 52 || j < 0 || j >= 52 {
655 b.Fatalf("bad swap(%d, %d)", i, j)
661 func BenchmarkRead3(b *testing.B) {
662 r := New(NewSource(1))
663 buf := make([]byte, 3)
665 for n := b.N; n > 0; n-- {
670 func BenchmarkRead64(b *testing.B) {
671 r := New(NewSource(1))
672 buf := make([]byte, 64)
674 for n := b.N; n > 0; n-- {
679 func BenchmarkRead1000(b *testing.B) {
680 r := New(NewSource(1))
681 buf := make([]byte, 1000)
683 for n := b.N; n > 0; n-- {
688 func BenchmarkConcurrent(b *testing.B) {
690 var wg sync.WaitGroup
692 for i := 0; i < goroutines; i++ {
695 for n := b.N; n > 0; n-- {