Skip to content

Commit 525a07c

Browse files
authored
Merge pull request #148 from boostorg/seedseq
Fix xoshiro constructors using seed seq incorrectly
2 parents 49d131b + c81bc32 commit 525a07c

File tree

9 files changed

+69
-11
lines changed

9 files changed

+69
-11
lines changed

include/boost/random/detail/xoshiro_base.hpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@ class xoshiro_base
5353
template <typename Sseq>
5454
inline void sseq_seed_64(Sseq& seq)
5555
{
56-
for (auto& i : state_)
57-
{
58-
std::array<std::uint32_t, 2> seeds;
59-
seq.generate(seeds.begin(), seeds.end());
56+
std::array<std::uint32_t, N * 2> seeds;
57+
seq.generate(seeds.begin(), seeds.end());
6058

61-
i = concatenate(seeds[0], seeds[1]);
59+
for (std::size_t i = 0; i < state_.size(); ++i)
60+
{
61+
state_[i] = concatenate(seeds[2*i], seeds[2*i + 1]);
6262
}
6363
}
6464

test/Jamfile.v2

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ run test_xoshiro128f.cpp /boost/test//boost_unit_test_framework ;
8787
run test_comp_xoshiro128f.cpp ;
8888

8989
run github_issue_133.cpp ;
90+
run github_issue_147.cpp ;
9091

9192
run niederreiter_base2_validate.cpp /boost/test//boost_unit_test_framework ;
9293
run sobol_validate.cpp /boost/test//boost_unit_test_framework ;

test/github_issue_147.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright Matt Borland 2025.
3+
* Distributed under the Boost Software License, Version 1.0. (See
4+
* accompanying file LICENSE_1_0.txt or copy at
5+
* http://www.boost.org/LICENSE_1_0.txt)
6+
*
7+
* See http://www.boost.org for most recent version including documentation.
8+
*
9+
* $Id$
10+
*/
11+
12+
#include <boost/random.hpp>
13+
#include <boost/core/lightweight_test.hpp>
14+
#include <array>
15+
#include <vector>
16+
#include <random>
17+
#include <cstdint>
18+
19+
template <class Arr>
20+
bool all_words_equal(const Arr& a)
21+
{
22+
for (std::size_t i = 1; i < a.size(); ++i)
23+
{
24+
if (a[i] != a[0]) return false;
25+
}
26+
27+
return true;
28+
}
29+
30+
template <class Engine, class SSeq>
31+
void test_engine_with_sseq(SSeq& sseq)
32+
{
33+
Engine eng(sseq);
34+
auto st = eng.state();
35+
BOOST_TEST(!all_words_equal(st));
36+
}
37+
38+
int main()
39+
{
40+
const std::vector<std::uint32_t> seed_words = {
41+
0x12345678u, 0x9abcdef0u, 0xc0ffee12u, 0xdeadbeefu
42+
};
43+
44+
// xoshiro128mm (4 x 32-bit state)
45+
std::seed_seq stdseq(seed_words.begin(), seed_words.end());
46+
boost::random::seed_seq bseq(seed_words.begin(), seed_words.end());
47+
test_engine_with_sseq<boost::random::xoshiro128mm>(stdseq);
48+
test_engine_with_sseq<boost::random::xoshiro128mm>(bseq);
49+
50+
// xoshiro256mm (4 x 64-bit state)
51+
std::seed_seq stdseq2(seed_words.begin(), seed_words.end());
52+
boost::random::seed_seq bseq2(seed_words.begin(), seed_words.end());
53+
test_engine_with_sseq<boost::random::xoshiro256mm>(stdseq2);
54+
test_engine_with_sseq<boost::random::xoshiro256mm>(bseq2);
55+
56+
return boost::report_errors();
57+
}

test/test_xoshiro256d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
// principal operation validated with CLHEP, values by experiment
2121
#define BOOST_RANDOM_VALIDATION_VALUE 0.91719108108351499
22-
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE 0.34930769688746899
22+
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE 0.68650209712672527
2323

2424
// Since we are using splitmix64 we need to allow 64 bit seeds
2525
// The test harness only allows for 32 bit seeds

test/test_xoshiro256mm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
// principal operation validated with CLHEP, values by experiment
2121
#define BOOST_RANDOM_VALIDATION_VALUE UINT64_C(2196391076106727935)
22-
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(3823370830110671407)
22+
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(8340052881247508375)
2323

2424
#include "test_generator.ipp"

test/test_xoshiro256pp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
// principal operation validated with CLHEP, values by experiment
2121
#define BOOST_RANDOM_VALIDATION_VALUE UINT64_C(8911602566162972150)
22-
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(11693002297289060464)
22+
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(9091783836875527177)
2323

2424
#include "test_generator.ipp"

test/test_xoshiro512d.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
// principal operation validated with CLHEP, values by experiment
2121
#define BOOST_RANDOM_VALIDATION_VALUE 0.85594919700533156
22-
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE 0.25120475433393952
22+
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE 0.77731098639989704
2323

2424
// Since we are using splitmix64 we need to allow 64 bit seeds
2525
// The test harness only allows for 32 bit seeds

test/test_xoshiro512mm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
// principal operation validated with CLHEP, values by experiment
2121
#define BOOST_RANDOM_VALIDATION_VALUE UINT64_C(9446215307655316885)
22-
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(13183137209047681026)
22+
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(7700017102361224222)
2323

2424
#include "test_generator.ipp"

test/test_xoshiro512pp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,6 @@
1919

2020
// principal operation validated with CLHEP, values by experiment
2121
#define BOOST_RANDOM_VALIDATION_VALUE UINT64_C(11685388408145467864)
22-
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(15400500895396352743)
22+
#define BOOST_RANDOM_SEED_SEQ_VALIDATION_VALUE UINT64_C(6773570493308843014)
2323

2424
#include "test_generator.ipp"

0 commit comments

Comments
 (0)