Home | History | Annotate | Download | only in rand
      1 // Copyright 2014 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.
      4 
      5 // Test that random number sequences generated by a specific seed
      6 // do not change from version to version.
      7 //
      8 // Do NOT make changes to the golden outputs. If bugs need to be fixed
      9 // in the underlying code, find ways to fix them that do not affect the
     10 // outputs.
     11 
     12 package rand_test
     13 
     14 import (
     15 	"flag"
     16 	"fmt"
     17 	. "math/rand"
     18 	"reflect"
     19 	"testing"
     20 )
     21 
     22 var printgolden = flag.Bool("printgolden", false, "print golden results for regression test")
     23 
     24 func TestRegress(t *testing.T) {
     25 	var int32s = []int32{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1}
     26 	var int64s = []int64{1, 10, 32, 1 << 20, 1<<20 + 1, 1000000000, 1 << 30, 1<<31 - 2, 1<<31 - 1, 1000000000000000000, 1 << 60, 1<<63 - 2, 1<<63 - 1}
     27 	var permSizes = []int{0, 1, 5, 8, 9, 10, 16}
     28 	r := New(NewSource(0))
     29 
     30 	rv := reflect.ValueOf(r)
     31 	n := rv.NumMethod()
     32 	p := 0
     33 	if *printgolden {
     34 		fmt.Printf("var regressGolden = []interface{}{\n")
     35 	}
     36 	for i := 0; i < n; i++ {
     37 		m := rv.Type().Method(i)
     38 		mv := rv.Method(i)
     39 		mt := mv.Type()
     40 		if mt.NumOut() == 0 {
     41 			continue
     42 		}
     43 		if mt.NumOut() != 1 {
     44 			t.Fatalf("unexpected result count for r.%s", m.Name)
     45 		}
     46 		r.Seed(0)
     47 		for repeat := 0; repeat < 20; repeat++ {
     48 			var args []reflect.Value
     49 			var argstr string
     50 			if mt.NumIn() == 1 {
     51 				var x interface{}
     52 				switch mt.In(0).Kind() {
     53 				default:
     54 					t.Fatalf("unexpected argument type for r.%s", m.Name)
     55 
     56 				case reflect.Int:
     57 					if m.Name == "Perm" {
     58 						x = permSizes[repeat%len(permSizes)]
     59 						break
     60 					}
     61 					big := int64s[repeat%len(int64s)]
     62 					if int64(int(big)) != big {
     63 						r.Int63n(big) // what would happen on 64-bit machine, to keep stream in sync
     64 						if *printgolden {
     65 							fmt.Printf("\tskipped, // must run printgolden on 64-bit machine\n")
     66 						}
     67 						p++
     68 						continue
     69 					}
     70 					x = int(big)
     71 
     72 				case reflect.Int32:
     73 					x = int32s[repeat%len(int32s)]
     74 
     75 				case reflect.Int64:
     76 					x = int64s[repeat%len(int64s)]
     77 				}
     78 				argstr = fmt.Sprint(x)
     79 				args = append(args, reflect.ValueOf(x))
     80 			}
     81 			out := mv.Call(args)[0].Interface()
     82 			if m.Name == "Int" || m.Name == "Intn" {
     83 				out = int64(out.(int))
     84 			}
     85 			if *printgolden {
     86 				var val string
     87 				big := int64(1 << 60)
     88 				if int64(int(big)) != big && (m.Name == "Int" || m.Name == "Intn") {
     89 					// 32-bit machine cannot print 64-bit results
     90 					val = "truncated"
     91 				} else if reflect.TypeOf(out).Kind() == reflect.Slice {
     92 					val = fmt.Sprintf("%#v", out)
     93 				} else {
     94 					val = fmt.Sprintf("%T(%v)", out, out)
     95 				}
     96 				fmt.Printf("\t%s, // %s(%s)\n", val, m.Name, argstr)
     97 			} else {
     98 				want := regressGolden[p]
     99 				if m.Name == "Int" {
    100 					want = int64(int(uint(want.(int64)) << 1 >> 1))
    101 				}
    102 				if !reflect.DeepEqual(out, want) {
    103 					t.Errorf("r.%s(%s) = %v, want %v", m.Name, argstr, out, want)
    104 				}
    105 			}
    106 			p++
    107 		}
    108 	}
    109 	if *printgolden {
    110 		fmt.Printf("}\n")
    111 	}
    112 }
    113 
    114 var regressGolden = []interface{}{
    115 	float64(4.668112973579268),    // ExpFloat64()
    116 	float64(0.1601593871172866),   // ExpFloat64()
    117 	float64(3.0465834105636),      // ExpFloat64()
    118 	float64(0.06385839451671879),  // ExpFloat64()
    119 	float64(1.8578917487258961),   // ExpFloat64()
    120 	float64(0.784676123472182),    // ExpFloat64()
    121 	float64(0.11225477361256932),  // ExpFloat64()
    122 	float64(0.20173283329802255),  // ExpFloat64()
    123 	float64(0.3468619496201105),   // ExpFloat64()
    124 	float64(0.35601103454384536),  // ExpFloat64()
    125 	float64(0.888376329507869),    // ExpFloat64()
    126 	float64(1.4081362450365698),   // ExpFloat64()
    127 	float64(1.0077753823151994),   // ExpFloat64()
    128 	float64(0.23594100766227588),  // ExpFloat64()
    129 	float64(2.777245612300007),    // ExpFloat64()
    130 	float64(0.5202997830662377),   // ExpFloat64()
    131 	float64(1.2842705247770294),   // ExpFloat64()
    132 	float64(0.030307408362776206), // ExpFloat64()
    133 	float64(2.204156824853721),    // ExpFloat64()
    134 	float64(2.09891923895058),     // ExpFloat64()
    135 	float32(0.94519615),           // Float32()
    136 	float32(0.24496509),           // Float32()
    137 	float32(0.65595627),           // Float32()
    138 	float32(0.05434384),           // Float32()
    139 	float32(0.3675872),            // Float32()
    140 	float32(0.28948045),           // Float32()
    141 	float32(0.1924386),            // Float32()
    142 	float32(0.65533215),           // Float32()
    143 	float32(0.8971697),            // Float32()
    144 	float32(0.16735445),           // Float32()
    145 	float32(0.28858566),           // Float32()
    146 	float32(0.9026048),            // Float32()
    147 	float32(0.84978026),           // Float32()
    148 	float32(0.2730468),            // Float32()
    149 	float32(0.6090802),            // Float32()
    150 	float32(0.253656),             // Float32()
    151 	float32(0.7746542),            // Float32()
    152 	float32(0.017480763),          // Float32()
    153 	float32(0.78707397),           // Float32()
    154 	float32(0.7993937),            // Float32()
    155 	float64(0.9451961492941164),   // Float64()
    156 	float64(0.24496508529377975),  // Float64()
    157 	float64(0.6559562651954052),   // Float64()
    158 	float64(0.05434383959970039),  // Float64()
    159 	float64(0.36758720663245853),  // Float64()
    160 	float64(0.2894804331565928),   // Float64()
    161 	float64(0.19243860967493215),  // Float64()
    162 	float64(0.6553321508148324),   // Float64()
    163 	float64(0.897169713149801),    // Float64()
    164 	float64(0.16735444255905835),  // Float64()
    165 	float64(0.2885856518054551),   // Float64()
    166 	float64(0.9026048462705047),   // Float64()
    167 	float64(0.8497802817628735),   // Float64()
    168 	float64(0.2730468047134829),   // Float64()
    169 	float64(0.6090801919903561),   // Float64()
    170 	float64(0.25365600644283687),  // Float64()
    171 	float64(0.7746542391859803),   // Float64()
    172 	float64(0.017480762156647272), // Float64()
    173 	float64(0.7870739563039942),   // Float64()
    174 	float64(0.7993936979594545),   // Float64()
    175 	int64(8717895732742165505),    // Int()
    176 	int64(2259404117704393152),    // Int()
    177 	int64(6050128673802995827),    // Int()
    178 	int64(501233450539197794),     // Int()
    179 	int64(3390393562759376202),    // Int()
    180 	int64(2669985732393126063),    // Int()
    181 	int64(1774932891286980153),    // Int()
    182 	int64(6044372234677422456),    // Int()
    183 	int64(8274930044578894929),    // Int()
    184 	int64(1543572285742637646),    // Int()
    185 	int64(2661732831099943416),    // Int()
    186 	int64(8325060299420976708),    // Int()
    187 	int64(7837839688282259259),    // Int()
    188 	int64(2518412263346885298),    // Int()
    189 	int64(5617773211005988520),    // Int()
    190 	int64(2339563716805116249),    // Int()
    191 	int64(7144924247938981575),    // Int()
    192 	int64(161231572858529631),     // Int()
    193 	int64(7259475919510918339),    // Int()
    194 	int64(7373105480197164748),    // Int()
    195 	int32(2029793274),             // Int31()
    196 	int32(526058514),              // Int31()
    197 	int32(1408655353),             // Int31()
    198 	int32(116702506),              // Int31()
    199 	int32(789387515),              // Int31()
    200 	int32(621654496),              // Int31()
    201 	int32(413258767),              // Int31()
    202 	int32(1407315077),             // Int31()
    203 	int32(1926657288),             // Int31()
    204 	int32(359390928),              // Int31()
    205 	int32(619732968),              // Int31()
    206 	int32(1938329147),             // Int31()
    207 	int32(1824889259),             // Int31()
    208 	int32(586363548),              // Int31()
    209 	int32(1307989752),             // Int31()
    210 	int32(544722126),              // Int31()
    211 	int32(1663557311),             // Int31()
    212 	int32(37539650),               // Int31()
    213 	int32(1690228450),             // Int31()
    214 	int32(1716684894),             // Int31()
    215 	int32(0),                      // Int31n(1)
    216 	int32(4),                      // Int31n(10)
    217 	int32(25),                     // Int31n(32)
    218 	int32(310570),                 // Int31n(1048576)
    219 	int32(857611),                 // Int31n(1048577)
    220 	int32(621654496),              // Int31n(1000000000)
    221 	int32(413258767),              // Int31n(1073741824)
    222 	int32(1407315077),             // Int31n(2147483646)
    223 	int32(1926657288),             // Int31n(2147483647)
    224 	int32(0),                      // Int31n(1)
    225 	int32(8),                      // Int31n(10)
    226 	int32(27),                     // Int31n(32)
    227 	int32(367019),                 // Int31n(1048576)
    228 	int32(209005),                 // Int31n(1048577)
    229 	int32(307989752),              // Int31n(1000000000)
    230 	int32(544722126),              // Int31n(1073741824)
    231 	int32(1663557311),             // Int31n(2147483646)
    232 	int32(37539650),               // Int31n(2147483647)
    233 	int32(0),                      // Int31n(1)
    234 	int32(4),                      // Int31n(10)
    235 	int64(8717895732742165505),    // Int63()
    236 	int64(2259404117704393152),    // Int63()
    237 	int64(6050128673802995827),    // Int63()
    238 	int64(501233450539197794),     // Int63()
    239 	int64(3390393562759376202),    // Int63()
    240 	int64(2669985732393126063),    // Int63()
    241 	int64(1774932891286980153),    // Int63()
    242 	int64(6044372234677422456),    // Int63()
    243 	int64(8274930044578894929),    // Int63()
    244 	int64(1543572285742637646),    // Int63()
    245 	int64(2661732831099943416),    // Int63()
    246 	int64(8325060299420976708),    // Int63()
    247 	int64(7837839688282259259),    // Int63()
    248 	int64(2518412263346885298),    // Int63()
    249 	int64(5617773211005988520),    // Int63()
    250 	int64(2339563716805116249),    // Int63()
    251 	int64(7144924247938981575),    // Int63()
    252 	int64(161231572858529631),     // Int63()
    253 	int64(7259475919510918339),    // Int63()
    254 	int64(7373105480197164748),    // Int63()
    255 	int64(0),                      // Int63n(1)
    256 	int64(2),                      // Int63n(10)
    257 	int64(19),                     // Int63n(32)
    258 	int64(959842),                 // Int63n(1048576)
    259 	int64(688912),                 // Int63n(1048577)
    260 	int64(393126063),              // Int63n(1000000000)
    261 	int64(89212473),               // Int63n(1073741824)
    262 	int64(834026388),              // Int63n(2147483646)
    263 	int64(1577188963),             // Int63n(2147483647)
    264 	int64(543572285742637646),     // Int63n(1000000000000000000)
    265 	int64(355889821886249464),     // Int63n(1152921504606846976)
    266 	int64(8325060299420976708),    // Int63n(9223372036854775806)
    267 	int64(7837839688282259259),    // Int63n(9223372036854775807)
    268 	int64(0),                      // Int63n(1)
    269 	int64(0),                      // Int63n(10)
    270 	int64(25),                     // Int63n(32)
    271 	int64(679623),                 // Int63n(1048576)
    272 	int64(882178),                 // Int63n(1048577)
    273 	int64(510918339),              // Int63n(1000000000)
    274 	int64(782454476),              // Int63n(1073741824)
    275 	int64(0),                      // Intn(1)
    276 	int64(4),                      // Intn(10)
    277 	int64(25),                     // Intn(32)
    278 	int64(310570),                 // Intn(1048576)
    279 	int64(857611),                 // Intn(1048577)
    280 	int64(621654496),              // Intn(1000000000)
    281 	int64(413258767),              // Intn(1073741824)
    282 	int64(1407315077),             // Intn(2147483646)
    283 	int64(1926657288),             // Intn(2147483647)
    284 	int64(543572285742637646),     // Intn(1000000000000000000)
    285 	int64(355889821886249464),     // Intn(1152921504606846976)
    286 	int64(8325060299420976708),    // Intn(9223372036854775806)
    287 	int64(7837839688282259259),    // Intn(9223372036854775807)
    288 	int64(0),                      // Intn(1)
    289 	int64(2),                      // Intn(10)
    290 	int64(14),                     // Intn(32)
    291 	int64(515775),                 // Intn(1048576)
    292 	int64(839455),                 // Intn(1048577)
    293 	int64(690228450),              // Intn(1000000000)
    294 	int64(642943070),              // Intn(1073741824)
    295 	float64(-0.28158587086436215), // NormFloat64()
    296 	float64(0.570933095808067),    // NormFloat64()
    297 	float64(-1.6920196326157044),  // NormFloat64()
    298 	float64(0.1996229111693099),   // NormFloat64()
    299 	float64(1.9195199291234621),   // NormFloat64()
    300 	float64(0.8954838794918353),   // NormFloat64()
    301 	float64(0.41457072128813166),  // NormFloat64()
    302 	float64(-0.48700161491544713), // NormFloat64()
    303 	float64(-0.1684059662402393),  // NormFloat64()
    304 	float64(0.37056410998929545),  // NormFloat64()
    305 	float64(1.0156889027029008),   // NormFloat64()
    306 	float64(-0.5174422210625114),  // NormFloat64()
    307 	float64(-0.5565834214413804),  // NormFloat64()
    308 	float64(0.778320596648391),    // NormFloat64()
    309 	float64(-1.8970718197702225),  // NormFloat64()
    310 	float64(0.5229525761688676),   // NormFloat64()
    311 	float64(-1.5515595563231523),  // NormFloat64()
    312 	float64(0.0182029289376123),   // NormFloat64()
    313 	float64(-0.6820951356608795),  // NormFloat64()
    314 	float64(-0.5987943422687668),  // NormFloat64()
    315 	[]int{},                                                     // Perm(0)
    316 	[]int{0},                                                    // Perm(1)
    317 	[]int{0, 4, 1, 3, 2},                                        // Perm(5)
    318 	[]int{3, 1, 0, 4, 7, 5, 2, 6},                               // Perm(8)
    319 	[]int{5, 0, 3, 6, 7, 4, 2, 1, 8},                            // Perm(9)
    320 	[]int{4, 5, 0, 2, 6, 9, 3, 1, 8, 7},                         // Perm(10)
    321 	[]int{14, 2, 0, 8, 3, 5, 13, 12, 1, 4, 6, 7, 11, 9, 15, 10}, // Perm(16)
    322 	[]int{},                                                     // Perm(0)
    323 	[]int{0},                                                    // Perm(1)
    324 	[]int{3, 0, 1, 2, 4},                                        // Perm(5)
    325 	[]int{5, 1, 2, 0, 4, 7, 3, 6},                               // Perm(8)
    326 	[]int{4, 0, 6, 8, 1, 5, 2, 7, 3},                            // Perm(9)
    327 	[]int{8, 6, 1, 7, 5, 4, 3, 2, 9, 0},                         // Perm(10)
    328 	[]int{0, 3, 13, 2, 15, 4, 10, 1, 8, 14, 7, 6, 12, 9, 5, 11}, // Perm(16)
    329 	[]int{},                             // Perm(0)
    330 	[]int{0},                            // Perm(1)
    331 	[]int{0, 4, 2, 1, 3},                // Perm(5)
    332 	[]int{2, 1, 7, 0, 6, 3, 4, 5},       // Perm(8)
    333 	[]int{8, 7, 5, 3, 4, 6, 0, 1, 2},    // Perm(9)
    334 	[]int{1, 0, 2, 5, 7, 6, 9, 8, 3, 4}, // Perm(10)
    335 	uint32(4059586549),                  // Uint32()
    336 	uint32(1052117029),                  // Uint32()
    337 	uint32(2817310706),                  // Uint32()
    338 	uint32(233405013),                   // Uint32()
    339 	uint32(1578775030),                  // Uint32()
    340 	uint32(1243308993),                  // Uint32()
    341 	uint32(826517535),                   // Uint32()
    342 	uint32(2814630155),                  // Uint32()
    343 	uint32(3853314576),                  // Uint32()
    344 	uint32(718781857),                   // Uint32()
    345 	uint32(1239465936),                  // Uint32()
    346 	uint32(3876658295),                  // Uint32()
    347 	uint32(3649778518),                  // Uint32()
    348 	uint32(1172727096),                  // Uint32()
    349 	uint32(2615979505),                  // Uint32()
    350 	uint32(1089444252),                  // Uint32()
    351 	uint32(3327114623),                  // Uint32()
    352 	uint32(75079301),                    // Uint32()
    353 	uint32(3380456901),                  // Uint32()
    354 	uint32(3433369789),                  // Uint32()
    355 }
    356