github.com/jingcheng-WU/gonum@v0.9.1-0.20210323123734-f1a2a11a8f7b/mathext/prng/prng_di_unimi_test.go (about) 1 // Copyright ©2019 The Gonum 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 package prng 6 7 import ( 8 "testing" 9 "time" 10 11 "golang.org/x/exp/rand" 12 ) 13 14 // Random values in tests are produced by 40 iterations of the C code. 15 16 var _ rand.Source = (*SplitMix64)(nil) 17 18 func TestSplitMix64(t *testing.T) { 19 t.Parallel() 20 want := []uint64{ 21 10451216379200822465, 13757245211066428519, 17911839290282890590, 8196980753821780235, 8195237237126968761, 22 14072917602864530048, 16184226688143867045, 9648886400068060533, 5266705631892356520, 14646652180046636950, 23 7455107161863376737, 11168034603498703870, 8392123148533390784, 9778231605760336522, 8042142155559163816, 24 3081251696030599739, 11904322950028659555, 15040563541741120241, 12575237177726700014, 16312908901713405192, 25 1216750802008901446, 1501835286251455644, 9147370558249537485, 2270958130545493676, 5292580334274787743, 26 883620860755687159, 9509663594007654709, 13166747327335888811, 807013244984872231, 18405200023706498954, 27 11028426030083068036, 10820770463232788922, 7326479631639850093, 8097875853865443356, 4672064935750269975, 28 9772298966463872780, 10028955912863736053, 13802505617680978881, 15090054588401425688, 12333003614474408764, 29 } 30 31 sm := NewSplitMix64(1) 32 for i := range want { 33 got := sm.Uint64() 34 if got != want[i] { 35 t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i]) 36 } 37 } 38 } 39 40 func TestSplitMix64RoundTrip(t *testing.T) { 41 t.Parallel() 42 var src SplitMix64 43 src.Seed(uint64(time.Now().Unix())) 44 45 buf, err := src.MarshalBinary() 46 if err != nil { 47 t.Errorf("unexpected error marshaling state: %v", err) 48 } 49 50 var dst SplitMix64 51 // Get dst into a non-zero state. 52 dst.Seed(1) 53 for i := 0; i < 10; i++ { 54 dst.Uint64() 55 } 56 57 err = dst.UnmarshalBinary(buf) 58 if err != nil { 59 t.Errorf("unexpected error unmarshaling state: %v", err) 60 } 61 62 if dst != src { 63 t.Errorf("mismatch between generator states: got:%+v want:%+v", dst, src) 64 } 65 } 66 67 var _ rand.Source = (*Xoshiro256plus)(nil) 68 69 func TestXoshiro256plus(t *testing.T) { 70 t.Parallel() 71 want := []uint64{ 72 201453059313051084, 16342930563397888806, 2922809869868169223, 13315230553875954649, 6410977891529050008, 73 2721661332018190285, 3769995280709464022, 17208995829377771030, 16938999919058283733, 8307416726322109393, 74 13997290115667311691, 5498422487743993519, 13193129985428835789, 17178224140053183722, 3371202013665523682, 75 6673444001875245482, 11649545741795472859, 4657392542380076879, 8631341306563158492, 16151880809814987639, 76 15271080878658922261, 6998002807989632655, 11431762507643441726, 136605885039865329, 16072241235209520170, 77 17064623797431990278, 6319393334343723778, 3599071131527455911, 14678971584471326753, 11566847267978507055, 78 37242444495476935, 9767625399998905638, 14799351402198708144, 15147234459691564338, 10081976988475685812, 79 12402022881820243150, 17939631254687971868, 15680836376982110901, 179319489669050051, 16194215847106809765, 80 } 81 82 xsr := NewXoshiro256plus(1) 83 for i := range want { 84 got := xsr.Uint64() 85 if got != want[i] { 86 t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i]) 87 } 88 } 89 } 90 91 func TestXoshiro256plusRoundTrip(t *testing.T) { 92 t.Parallel() 93 var src Xoshiro256plus 94 src.Seed(uint64(time.Now().Unix())) 95 96 src.Uint64() // Step PRNG once to makes sure states are mixed. 97 98 buf, err := src.MarshalBinary() 99 if err != nil { 100 t.Errorf("unexpected error marshaling state: %v", err) 101 } 102 103 var dst Xoshiro256plus 104 // Get dst into a non-zero state. 105 dst.Seed(1) 106 for i := 0; i < 10; i++ { 107 dst.Uint64() 108 } 109 110 err = dst.UnmarshalBinary(buf) 111 if err != nil { 112 t.Errorf("unexpected error unmarshaling state: %v", err) 113 } 114 115 if dst != src { 116 t.Errorf("mismatch between generator states: got:%+v want:%+v", dst, src) 117 } 118 } 119 120 var _ rand.Source = (*Xoshiro256plusplus)(nil) 121 122 func TestXoshiro256plusplus(t *testing.T) { 123 t.Parallel() 124 want := []uint64{ 125 14971601782005023387, 13781649495232077965, 1847458086238483744, 13765271635752736470, 3406718355780431780, 126 10892412867582108485, 18204613561675945223, 9655336933892813345, 1781989159761824720, 2477283028068920342, 127 16978024111547606601, 6336475467619303347, 1336129645694042326, 7278725533440954441, 1650926874576718010, 128 2884092293074692283, 10277292511068429730, 8723528388573605619, 17670016435951889822, 11847526622624223050, 129 4869519043768407819, 14645621260580619786, 2927941368235978475, 7627105703721172900, 4384663367605854827, 130 11119034730948704880, 3397900810577180010, 18115970067406137490, 11274606161466886392, 13467911786374401590, 131 10949103424463861935, 11981483663808188895, 9358210361682609782, 11442939244776437245, 17602980262171424054, 132 5959474180322755185, 1996769245947054333, 13544632058761996522, 16649296193330087156, 12760326241867116135, 133 } 134 135 xsr := NewXoshiro256plusplus(1) 136 for i := range want { 137 got := xsr.Uint64() 138 if got != want[i] { 139 t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i]) 140 } 141 } 142 } 143 144 func TestXoshiro256plusplusRoundTrip(t *testing.T) { 145 t.Parallel() 146 var src Xoshiro256plusplus 147 src.Seed(uint64(time.Now().Unix())) 148 149 src.Uint64() // Step PRNG once to makes sure states are mixed. 150 151 buf, err := src.MarshalBinary() 152 if err != nil { 153 t.Errorf("unexpected error marshaling state: %v", err) 154 } 155 156 var dst Xoshiro256plusplus 157 // Get dst into a non-zero state. 158 dst.Seed(1) 159 for i := 0; i < 10; i++ { 160 dst.Uint64() 161 } 162 163 err = dst.UnmarshalBinary(buf) 164 if err != nil { 165 t.Errorf("unexpected error unmarshaling state: %v", err) 166 } 167 168 if dst != src { 169 t.Errorf("mismatch between generator states: got:%+v want:%+v", dst, src) 170 } 171 } 172 173 var _ rand.Source = (*Xoshiro256starstar)(nil) 174 175 func TestXoshiro256starstar(t *testing.T) { 176 t.Parallel() 177 want := []uint64{ 178 12966619160104079557, 9600361134598540522, 10590380919521690900, 7218738570589545383, 12860671823995680371, 179 2648436617965840162, 1310552918490157286, 7031611932980406429, 15996139959407692321, 10177250653276320208, 180 17202925169076741841, 17657558547222227110, 17206619296382044401, 12342657103067243573, 11066818095355039191, 181 16427605434558419749, 1484150211974036615, 9063990983673329711, 845232928428614080, 1176429380546917807, 182 8545088851120551825, 9158324580728115577, 11267126437916202177, 6452051665337041730, 7460617819096774474, 183 3909615622106851260, 7148019177890935463, 15761474764570999248, 13856144421012645925, 18119237044791779759, 184 202581184499657049, 16256128138147959276, 7894450248801719761, 7285265299121834259, 11974578372788407364, 185 4350246478179107086, 4560570958642824732, 15448532239578831742, 7084622563335324071, 8654072644765974953, 186 } 187 188 xsr := NewXoshiro256starstar(1) 189 for i := range want { 190 got := xsr.Uint64() 191 if got != want[i] { 192 t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i]) 193 } 194 } 195 } 196 197 func TestXoshiro256starstarRoundTrip(t *testing.T) { 198 t.Parallel() 199 var src Xoshiro256starstar 200 src.Seed(uint64(time.Now().Unix())) 201 202 src.Uint64() // Step PRNG once to makes sure states are mixed. 203 204 buf, err := src.MarshalBinary() 205 if err != nil { 206 t.Errorf("unexpected error marshaling state: %v", err) 207 } 208 209 var dst Xoshiro256starstar 210 // Get dst into a non-zero state. 211 dst.Seed(1) 212 for i := 0; i < 10; i++ { 213 dst.Uint64() 214 } 215 216 err = dst.UnmarshalBinary(buf) 217 if err != nil { 218 t.Errorf("unexpected error unmarshaling state: %v", err) 219 } 220 221 if dst != src { 222 t.Errorf("mismatch between generator states: got:%+v want:%+v", dst, src) 223 } 224 }