gonum.org/v1/gonum@v0.14.0/mathext/prng/mt19937_64_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  var _ rand.Source = (*MT19937_64)(nil)
    15  
    16  // Random values in tests are produced by 40 iterations of the C code
    17  // with or without an initial seed array.
    18  
    19  func TestMT19937_64(t *testing.T) {
    20  	t.Parallel()
    21  	want := []uint64{
    22  		14514284786278117030, 4620546740167642908, 13109570281517897720, 17462938647148434322, 355488278567739596,
    23  		7469126240319926998, 4635995468481642529, 418970542659199878, 9604170989252516556, 6358044926049913402,
    24  		5058016125798318033, 10349215569089701407, 2583272014892537200, 10032373690199166667, 9627645531742285868,
    25  		15810285301089087632, 9219209713614924562, 7736011505917826031, 13729552270962724157, 4596340717661012313,
    26  		4413874586873285858, 5904155143473820934, 16795776195466785825, 3040631852046752166, 4529279813148173111,
    27  		3658352497551999605, 13205889818278417278, 17853215078830450730, 14193508720503142180, 1488787817663097441,
    28  		8484116316263611556, 4745643133208116498, 14333959900198994173, 10770733876927207790, 17529942701849009476,
    29  		8081518017574486547, 5945178879512507902, 9821139136195250096, 4728986788662773602, 840062144447779464,
    30  	}
    31  
    32  	mt := NewMT19937_64()
    33  	for i := range want {
    34  		got := mt.Uint64()
    35  		if got != want[i] {
    36  			t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i])
    37  		}
    38  	}
    39  }
    40  
    41  func TestMT19937_64SeedFromKeys(t *testing.T) {
    42  	t.Parallel()
    43  	want := []uint64{
    44  		7266447313870364031, 4946485549665804864, 16945909448695747420, 16394063075524226720, 4873882236456199058,
    45  		14877448043947020171, 6740343660852211943, 13857871200353263164, 5249110015610582907, 10205081126064480383,
    46  		1235879089597390050, 17320312680810499042, 16489141110565194782, 8942268601720066061, 13520575722002588570,
    47  		14226945236717732373, 9383926873555417063, 15690281668532552105, 11510704754157191257, 15864264574919463609,
    48  		6489677788245343319, 5112602299894754389, 10828930062652518694, 15942305434158995996, 15445717675088218264,
    49  		4764500002345775851, 14673753115101942098, 236502320419669032, 13670483975188204088, 14931360615268175698,
    50  		8904234204977263924, 12836915408046564963, 12120302420213647524, 15755110976537356441, 5405758943702519480,
    51  		10951858968426898805, 17251681303478610375, 4144140664012008120, 18286145806977825275, 13075804672185204371,
    52  	}
    53  
    54  	mt := NewMT19937_64()
    55  	mt.SeedFromKeys([]uint64{0x12345, 0x23456, 0x34567, 0x45678})
    56  	for i := range want {
    57  		got := mt.Uint64()
    58  		if got != want[i] {
    59  			t.Errorf("unexpected random value at iteration %d: got:%d want:%d", i, got, want[i])
    60  		}
    61  	}
    62  }
    63  
    64  func TestMT19937_64RoundTrip(t *testing.T) {
    65  	t.Parallel()
    66  	var src MT19937_64
    67  	src.Seed(uint64(time.Now().Unix()))
    68  
    69  	src.Uint64() // Step PRNG once to makes sure states are mixed.
    70  
    71  	buf, err := src.MarshalBinary()
    72  	if err != nil {
    73  		t.Errorf("unexpected error marshaling state: %v", err)
    74  	}
    75  
    76  	var dst MT19937_64
    77  	// Get dst into a non-zero state.
    78  	dst.Seed(1)
    79  	for i := 0; i < 10; i++ {
    80  		dst.Uint64()
    81  	}
    82  
    83  	err = dst.UnmarshalBinary(buf)
    84  	if err != nil {
    85  		t.Errorf("unexpected error unmarshaling state: %v", err)
    86  	}
    87  
    88  	if dst != src {
    89  		t.Errorf("mismatch between generator states: got:%+v want:%+v", dst, src)
    90  	}
    91  }