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.
21 numTestSamples = 10000
24 var rn, kn, wn, fn = GetNormalDistributionParameters()
25 var re, ke, we, fe = GetExponentialDistributionParameters()
27 type statsResults struct {
34 func max(a, b float64) float64 {
41 func nearEqual(a, b, closeEnough, maxError float64) bool {
42 absDiff := math.Abs(a - b)
43 if absDiff < closeEnough { // Necessary when one value is zero and one value is close to zero.
46 return absDiff/max(math.Abs(a), math.Abs(b)) < maxError
49 var testSeeds = []uint64{1, 1754801282, 1698661970, 1550503961}
51 // checkSimilarDistribution returns success if the mean and stddev of the
52 // two statsResults are similar.
53 func (this *statsResults) checkSimilarDistribution(expected *statsResults) error {
54 if !nearEqual(this.mean, expected.mean, expected.closeEnough, expected.maxError) {
55 s := fmt.Sprintf("mean %v != %v (allowed error %v, %v)", this.mean, expected.mean, expected.closeEnough, expected.maxError)
59 if !nearEqual(this.stddev, expected.stddev, expected.closeEnough, expected.maxError) {
60 s := fmt.Sprintf("stddev %v != %v (allowed error %v, %v)", this.stddev, expected.stddev, expected.closeEnough, expected.maxError)
67 func getStatsResults(samples []float64) *statsResults {
68 res := new(statsResults)
69 var sum, squaresum float64
70 for _, s := range samples {
74 res.mean = sum / float64(len(samples))
75 res.stddev = math.Sqrt(squaresum/float64(len(samples)) - res.mean*res.mean)
79 func checkSampleDistribution(t *testing.T, samples []float64, expected *statsResults) {
81 actual := getStatsResults(samples)
82 err := actual.checkSimilarDistribution(expected)
88 func checkSampleSliceDistributions(t *testing.T, samples []float64, nslices int, expected *statsResults) {
90 chunk := len(samples) / nslices
91 for i := 0; i < nslices; i++ {
95 high = len(samples) - 1
97 high = (i + 1) * chunk
99 checkSampleDistribution(t, samples[low:high], expected)
104 // Normal distribution tests
107 func generateNormalSamples(nsamples int, mean, stddev float64, seed uint64) []float64 {
108 r := New(NewPCG(seed, seed))
109 samples := make([]float64, nsamples)
110 for i := range samples {
111 samples[i] = r.NormFloat64()*stddev + mean
116 func testNormalDistribution(t *testing.T, nsamples int, mean, stddev float64, seed uint64) {
117 //fmt.Printf("testing nsamples=%v mean=%v stddev=%v seed=%v\n", nsamples, mean, stddev, seed);
119 samples := generateNormalSamples(nsamples, mean, stddev, seed)
120 errorScale := max(1.0, stddev) // Error scales with stddev
121 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.08 * errorScale}
123 // Make sure that the entire set matches the expected distribution.
124 checkSampleDistribution(t, samples, expected)
126 // Make sure that each half of the set matches the expected distribution.
127 checkSampleSliceDistributions(t, samples, 2, expected)
129 // Make sure that each 7th of the set matches the expected distribution.
130 checkSampleSliceDistributions(t, samples, 7, expected)
135 func TestStandardNormalValues(t *testing.T) {
136 for _, seed := range testSeeds {
137 testNormalDistribution(t, numTestSamples, 0, 1, seed)
141 func TestNonStandardNormalValues(t *testing.T) {
148 for sd := 0.5; sd < sdmax; sd *= 2 {
149 for m := 0.5; m < mmax; m *= 2 {
150 for _, seed := range testSeeds {
151 testNormalDistribution(t, numTestSamples, m, sd, seed)
161 // Exponential distribution tests
164 func generateExponentialSamples(nsamples int, rate float64, seed uint64) []float64 {
165 r := New(NewPCG(seed, seed))
166 samples := make([]float64, nsamples)
167 for i := range samples {
168 samples[i] = r.ExpFloat64() / rate
173 func testExponentialDistribution(t *testing.T, nsamples int, rate float64, seed uint64) {
174 //fmt.Printf("testing nsamples=%v rate=%v seed=%v\n", nsamples, rate, seed);
179 samples := generateExponentialSamples(nsamples, rate, seed)
180 errorScale := max(1.0, 1/rate) // Error scales with the inverse of the rate
181 expected := &statsResults{mean, stddev, 0.10 * errorScale, 0.20 * errorScale}
183 // Make sure that the entire set matches the expected distribution.
184 checkSampleDistribution(t, samples, expected)
186 // Make sure that each half of the set matches the expected distribution.
187 checkSampleSliceDistributions(t, samples, 2, expected)
189 // Make sure that each 7th of the set matches the expected distribution.
190 checkSampleSliceDistributions(t, samples, 7, expected)
195 func TestStandardExponentialValues(t *testing.T) {
196 for _, seed := range testSeeds {
197 testExponentialDistribution(t, numTestSamples, 1, seed)
201 func TestNonStandardExponentialValues(t *testing.T) {
202 for rate := 0.05; rate < 10; rate *= 2 {
203 for _, seed := range testSeeds {
204 testExponentialDistribution(t, numTestSamples, rate, seed)
213 // Table generation tests
216 func initNorm() (testKn []uint32, testWn, testFn []float32) {
221 vn float64 = 9.91256303526217e-3
224 testKn = make([]uint32, 128)
225 testWn = make([]float32, 128)
226 testFn = make([]float32, 128)
228 q := vn / math.Exp(-0.5*dn*dn)
229 testKn[0] = uint32((dn / q) * m1)
231 testWn[0] = float32(q / m1)
232 testWn[127] = float32(dn / m1)
234 testFn[127] = float32(math.Exp(-0.5 * dn * dn))
235 for i := 126; i >= 1; i-- {
236 dn = math.Sqrt(-2.0 * math.Log(vn/dn+math.Exp(-0.5*dn*dn)))
237 testKn[i+1] = uint32((dn / tn) * m1)
239 testFn[i] = float32(math.Exp(-0.5 * dn * dn))
240 testWn[i] = float32(dn / m1)
245 func initExp() (testKe []uint32, testWe, testFe []float32) {
250 ve float64 = 3.9496598225815571993e-3
253 testKe = make([]uint32, 256)
254 testWe = make([]float32, 256)
255 testFe = make([]float32, 256)
257 q := ve / math.Exp(-de)
258 testKe[0] = uint32((de / q) * m2)
260 testWe[0] = float32(q / m2)
261 testWe[255] = float32(de / m2)
263 testFe[255] = float32(math.Exp(-de))
264 for i := 254; i >= 1; i-- {
265 de = -math.Log(ve/de + math.Exp(-de))
266 testKe[i+1] = uint32((de / te) * m2)
268 testFe[i] = float32(math.Exp(-de))
269 testWe[i] = float32(de / m2)
274 // compareUint32Slices returns the first index where the two slices
275 // disagree, or <0 if the lengths are the same and all elements
277 func compareUint32Slices(s1, s2 []uint32) int {
278 if len(s1) != len(s2) {
279 if len(s1) > len(s2) {
292 // compareFloat32Slices returns the first index where the two slices
293 // disagree, or <0 if the lengths are the same and all elements
295 func compareFloat32Slices(s1, s2 []float32) int {
296 if len(s1) != len(s2) {
297 if len(s1) > len(s2) {
303 if !nearEqual(float64(s1[i]), float64(s2[i]), 0, 1e-7) {
310 func TestNormTables(t *testing.T) {
311 testKn, testWn, testFn := initNorm()
312 if i := compareUint32Slices(kn[0:], testKn); i >= 0 {
313 t.Errorf("kn disagrees at index %v; %v != %v", i, kn[i], testKn[i])
315 if i := compareFloat32Slices(wn[0:], testWn); i >= 0 {
316 t.Errorf("wn disagrees at index %v; %v != %v", i, wn[i], testWn[i])
318 if i := compareFloat32Slices(fn[0:], testFn); i >= 0 {
319 t.Errorf("fn disagrees at index %v; %v != %v", i, fn[i], testFn[i])
323 func TestExpTables(t *testing.T) {
324 testKe, testWe, testFe := initExp()
325 if i := compareUint32Slices(ke[0:], testKe); i >= 0 {
326 t.Errorf("ke disagrees at index %v; %v != %v", i, ke[i], testKe[i])
328 if i := compareFloat32Slices(we[0:], testWe); i >= 0 {
329 t.Errorf("we disagrees at index %v; %v != %v", i, we[i], testWe[i])
331 if i := compareFloat32Slices(fe[0:], testFe); i >= 0 {
332 t.Errorf("fe disagrees at index %v; %v != %v", i, fe[i], testFe[i])
336 func hasSlowFloatingPoint() bool {
337 switch runtime.GOARCH {
339 return os.Getenv("GOARM") == "5"
340 case "mips", "mipsle", "mips64", "mips64le":
341 // Be conservative and assume that all mips boards
342 // have emulated floating point.
343 // TODO: detect what it actually has.
349 func TestFloat32(t *testing.T) {
350 // For issue 6721, the problem came after 7533753 calls, so check 10e6.
352 // But do the full amount only on builders (not locally).
353 // But ARM5 floating point emulation is slow (Issue 10749), so
354 // do less for that builder:
355 if testing.Short() && (testenv.Builder() == "" || hasSlowFloatingPoint()) {
356 num /= 100 // 1.72 seconds instead of 172 seconds
360 for ct := 0; ct < num; ct++ {
363 t.Fatal("Float32() should be in range [0,1). ct:", ct, "f:", f)
368 func TestShuffleSmall(t *testing.T) {
369 // Check that Shuffle allows n=0 and n=1, but that swap is never called for them.
371 for n := 0; n <= 1; n++ {
372 r.Shuffle(n, func(i, j int) { t.Fatalf("swap called, n=%d i=%d j=%d", n, i, j) })
376 // encodePerm converts from a permuted slice of length n, such as Perm generates, to an int in [0, n!).
377 // See https://en.wikipedia.org/wiki/Lehmer_code.
378 // encodePerm modifies the input slice.
379 func encodePerm(s []int) int {
380 // Convert to Lehmer code.
381 for i, x := range s {
383 for j, y := range r {
389 // Convert to int in [0, n!).
392 for i := len(s) - 1; i >= 0; i-- {
399 // TestUniformFactorial tests several ways of generating a uniform value in [0, n!).
400 func TestUniformFactorial(t *testing.T) {
401 r := New(NewPCG(1, 2))
406 for n := 3; n <= top; n++ {
407 t.Run(fmt.Sprintf("n=%d", n), func(t *testing.T) {
410 for i := 2; i <= n; i++ {
414 // Test a few different ways to generate a uniform distribution.
415 p := make([]int, n) // re-usable slice for Shuffle generator
416 tests := [...]struct {
420 {name: "Int32N", fn: func() int { return int(r.Int32N(int32(nfact))) }},
421 {name: "Perm", fn: func() int { return encodePerm(r.Perm(n)) }},
422 {name: "Shuffle", fn: func() int {
423 // Generate permutation using Shuffle.
427 r.Shuffle(n, func(i, j int) { p[i], p[j] = p[j], p[i] })
432 for _, test := range tests {
433 t.Run(test.name, func(t *testing.T) {
434 // Gather chi-squared values and check that they follow
435 // the expected normal distribution given n!-1 degrees of freedom.
436 // See https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test and
437 // https://www.johndcook.com/Beautiful_Testing_ch10.pdf.
438 nsamples := 10 * nfact
442 samples := make([]float64, nsamples)
443 for i := range samples {
444 // Generate some uniformly distributed values and count their occurrences.
446 counts := make([]int, nfact)
447 for i := 0; i < iters; i++ {
450 // Calculate chi-squared and add to samples.
451 want := iters / float64(nfact)
453 for _, have := range counts {
454 err := float64(have) - want
461 // Check that our samples approximate the appropriate normal distribution.
462 dof := float64(nfact - 1)
463 expected := &statsResults{mean: dof, stddev: math.Sqrt(2 * dof)}
464 errorScale := max(1.0, expected.stddev)
465 expected.closeEnough = 0.10 * errorScale
466 expected.maxError = 0.08 // TODO: What is the right value here? See issue 21211.
467 checkSampleDistribution(t, samples, expected)
478 func testRand() *Rand {
479 return New(NewPCG(1, 2))
482 func BenchmarkSourceUint64(b *testing.B) {
485 for n := b.N; n > 0; n-- {
491 func BenchmarkGlobalInt64(b *testing.B) {
493 for n := b.N; n > 0; n-- {
499 func BenchmarkGlobalInt64Parallel(b *testing.B) {
500 b.RunParallel(func(pb *testing.PB) {
505 atomic.AddUint64(&Sink, uint64(t))
509 func BenchmarkGlobalUint64(b *testing.B) {
511 for n := b.N; n > 0; n-- {
517 func BenchmarkGlobalUint64Parallel(b *testing.B) {
518 b.RunParallel(func(pb *testing.PB) {
523 atomic.AddUint64(&Sink, t)
527 func BenchmarkInt64(b *testing.B) {
530 for n := b.N; n > 0; n-- {
536 var AlwaysFalse = false
538 func keep[T int | uint | int32 | uint32 | int64 | uint64](x T) T {
545 func BenchmarkUint64(b *testing.B) {
548 for n := b.N; n > 0; n-- {
554 func BenchmarkGlobalIntN1000(b *testing.B) {
557 for n := b.N; n > 0; n-- {
563 func BenchmarkIntN1000(b *testing.B) {
567 for n := b.N; n > 0; n-- {
573 func BenchmarkInt64N1000(b *testing.B) {
576 arg := keep(int64(1000))
577 for n := b.N; n > 0; n-- {
583 func BenchmarkInt64N1e8(b *testing.B) {
586 arg := keep(int64(1e8))
587 for n := b.N; n > 0; n-- {
593 func BenchmarkInt64N1e9(b *testing.B) {
596 arg := keep(int64(1e9))
597 for n := b.N; n > 0; n-- {
603 func BenchmarkInt64N2e9(b *testing.B) {
606 arg := keep(int64(2e9))
607 for n := b.N; n > 0; n-- {
613 func BenchmarkInt64N1e18(b *testing.B) {
616 arg := keep(int64(1e18))
617 for n := b.N; n > 0; n-- {
623 func BenchmarkInt64N2e18(b *testing.B) {
626 arg := keep(int64(2e18))
627 for n := b.N; n > 0; n-- {
633 func BenchmarkInt64N4e18(b *testing.B) {
636 arg := keep(int64(4e18))
637 for n := b.N; n > 0; n-- {
643 func BenchmarkInt32N1000(b *testing.B) {
646 arg := keep(int32(1000))
647 for n := b.N; n > 0; n-- {
653 func BenchmarkInt32N1e8(b *testing.B) {
656 arg := keep(int32(1e8))
657 for n := b.N; n > 0; n-- {
663 func BenchmarkInt32N1e9(b *testing.B) {
666 arg := keep(int32(1e9))
667 for n := b.N; n > 0; n-- {
673 func BenchmarkInt32N2e9(b *testing.B) {
676 arg := keep(int32(2e9))
677 for n := b.N; n > 0; n-- {
683 func BenchmarkFloat32(b *testing.B) {
686 for n := b.N; n > 0; n-- {
692 func BenchmarkFloat64(b *testing.B) {
695 for n := b.N; n > 0; n-- {
701 func BenchmarkExpFloat64(b *testing.B) {
704 for n := b.N; n > 0; n-- {
710 func BenchmarkNormFloat64(b *testing.B) {
713 for n := b.N; n > 0; n-- {
719 func BenchmarkPerm3(b *testing.B) {
722 for n := b.N; n > 0; n-- {
729 func BenchmarkPerm30(b *testing.B) {
732 for n := b.N; n > 0; n-- {
738 func BenchmarkPerm30ViaShuffle(b *testing.B) {
741 for n := b.N; n > 0; n-- {
746 r.Shuffle(30, func(i, j int) { p[i], p[j] = p[j], p[i] })
752 // BenchmarkShuffleOverhead uses a minimal swap function
753 // to measure just the shuffling overhead.
754 func BenchmarkShuffleOverhead(b *testing.B) {
756 for n := b.N; n > 0; n-- {
757 r.Shuffle(30, func(i, j int) {
758 if i < 0 || i >= 30 || j < 0 || j >= 30 {
759 b.Fatalf("bad swap(%d, %d)", i, j)
765 func BenchmarkConcurrent(b *testing.B) {
767 var wg sync.WaitGroup
769 for i := 0; i < goroutines; i++ {
772 for n := b.N; n > 0; n-- {
780 func TestN(t *testing.T) {
781 for i := 0; i < 1000; i++ {
783 if v < 0 || v >= 10 {
784 t.Fatalf("N(10) returned %d", v)