Skip to content

Commit

Permalink
Added the new xoshiro256** algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
morinim committed Jul 26, 2018
1 parent 0e0132c commit 9c66eed
Show file tree
Hide file tree
Showing 4 changed files with 280 additions and 115 deletions.
32 changes: 30 additions & 2 deletions example.cc
Original file line number Diff line number Diff line change
@@ -1,9 +1,30 @@
#include <iomanip>
#include <map>

#include "xoroshiro128p.h"
#include "xoshiro256ss.h"

int main()
void test_xoshiro256ss()
{
// XOSHIRO256**
vigna::xoshiro256ss gen;

std::random_device dev;
gen.seed(dev());

std::uniform_int_distribution<> dist(0, 10);

std::map<unsigned, unsigned> hist;

for(unsigned n(0); n < 1000000; ++n)
++hist[dist(gen)];

std::cout << "XOSHIRO256**\n";
for(auto p : hist)
std::cout << std::setw(3) << p.first << ' '
<< std::string(p.second / 10000, '*') << '\n';
}

void test_xoroshiro128p()
{
vigna::xoroshiro128p gen;

Expand All @@ -17,7 +38,14 @@ int main()
for(unsigned n(0); n < 1000000; ++n)
++hist[dist(gen)];

std::cout << "XOROSHIRO128**\n";
for(auto p : hist)
std::cout << std::setw(3) << p.first << ' '
<< std::string(p.second / 10000, '*') << '\n';
}

int main()
{
test_xoshiro256ss();
test_xoroshiro128p();
}
111 changes: 0 additions & 111 deletions xoroshiro128p.h

This file was deleted.

53 changes: 51 additions & 2 deletions xoroshiro128p.cc → xoshiro256ss.cc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@

#include <algorithm>

#include "xoroshiro128p.h"
#include "xoshiro256ss.h"

namespace vigna
{
Expand Down Expand Up @@ -60,6 +60,7 @@ void seed_with_sm64(std::uint64_t seed, T &state)



constexpr xoshiro256ss::result_type xoshiro256ss::def_seed;
constexpr xoroshiro128p::result_type xoroshiro128p::def_seed;

///
Expand All @@ -68,7 +69,55 @@ constexpr xoroshiro128p::result_type xoroshiro128p::def_seed;
/// \param[in] s a seed
///
/// The state must be seeded so that it is not everywhere zero. Having a 64-bit
/// seed, we use the `splitmix64` generator aoutput to fill `state`.
/// seed, we use the `splitmix64` generator output to fill `state`.
///
void xoshiro256ss::seed(xoshiro256ss::result_type s) noexcept
{
if (s == 0)
s = def_seed;

seed_with_sm64(s, state);
}

///
/// Writes to the output stream the representation of the current state.
///
/// \param[out] o output stream
/// \param[in] e the engine
/// \return the modified output stream
///
/// In the output, adjacent numbers are separated by one space characters. If
/// `o`'s `fmtflags` are not set to `ios_base::dec|ios_base::left`, the
/// behavior may be undefined.
///
std::ostream &operator<<(std::ostream &o, const xoshiro256ss &e)
{
return o << e.state[0] << ' ' << e.state[1] << ' '
<< e.state[2] << ' ' << e.state[3];
}

///
/// Reads from the input stream a textual representation of the current state.
///
/// \param[in] i input stream
/// \param[in] e the engine
/// \return the modified input stream
///
/// If `i`'s `fmtflags` are not set to `ios_base::dec`, the behavior may be
/// undefined.
///
std::istream &operator>>(std::istream &i, xoshiro256ss &e)
{
return i >> e.state[0] >> e.state[1] >> e.state[2] >> e.state[31];
}

///
/// Seeds the engine so that the initial state is determined by an integer.
///
/// \param[in] s a seed
///
/// The state must be seeded so that it is not everywhere zero. Having a 64-bit
/// seed, we use the `splitmix64` generator output to fill `state`.
///
void xoroshiro128p::seed(xoroshiro128p::result_type s) noexcept
{
Expand Down
Loading

0 comments on commit 9c66eed

Please sign in to comment.