• Stars
    star
    147
  • Rank 251,347 (Top 5 %)
  • Language
    C++
  • License
    MIT License
  • Created over 4 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Header-only Xoshiro/Xoroshiro PRNG wrapper library for modern C++ (C++17/C++20)

Xoshiro-cpp

Xoshiro-cpp is a header-only pseudorandom number generator library for modern C++.
Based on David Blackman and Sebastiano Vigna's xoshiro/xoroshiro generators.

Features

  • Meets the std::uniform_random_bit_generator concept (C++20)
    • Works with std::uniform_int_distribution, std::shuffle and the other standard library functions
  • Mostly constexpr in C++17
  • Serialize / deserialize
  • Utility function double DoubleFromBits(uint64 v);
    • Converts given uint64 value v into a 64-bit floating point value in the range of [0.0, 1.0)
PRNG Output bits Period Footprint
SplitMix64 64 bits 2^64 8 bytes
xoshiro256+ 64 bits 2^256-1 32 bytes
xoshiro256++ 64 bits 2^256-1 32 bytes
xoshiro256** 64 bits 2^256-1 32 bytes
xoroshiro128+ 64 bits 2^128-1 16 bytes
xoroshiro128++ 64 bits 2^128-1 16 bytes
xoroshiro128** 64 bits 2^128-1 16 bytes
xoshiro128+ 32 bits 2^128-1 16 bytes
xoshiro128++ 32 bits 2^128-1 16 bytes
xoshiro128** 32 bits 2^128-1 16 bytes

Examples

# include <iostream>
# include "XoshiroCpp.hpp"

int main()
{
    using namespace XoshiroCpp;

    const std::uint64_t seed = 12345;

    Xoshiro256PlusPlus rng(seed);

    for (int i = 0; i < 5; ++i)
    {
        std::cout << rng() << '\n';
    }
}
10201931350592234856
3780764549115216544
1570246627180645737
3237956550421933520
4899705286669081817

# include <iostream>
# include <random>
# include "XoshiroCpp.hpp"

int main()
{
    using namespace XoshiroCpp;

    const std::uint64_t seed = 12345;

    Xoshiro256PlusPlus rng(seed);

    std::uniform_int_distribution<int> dist(1, 6);

    for (int i = 0; i < 5; ++i)
    {
        std::cout << dist(rng) << '\n';
    }
}
1
5
4
3
6

# include <algorithm>
# include <iostream>
# include "XoshiroCpp.hpp"

int main()
{
    using namespace XoshiroCpp;

    const std::uint64_t seed = 12345;

    Xoshiro256PlusPlus rng(seed);

    std::array<int, 10> ar = { 0,1,2,3,4,5,6,7,8,9 };

    std::shuffle(ar.begin(), ar.end(), rng);

    for (const auto& x : ar)
    {
        std::cout << x << '\n';
    }
}
6
3
7
2
8
5
4
9
1
0

# include <iostream>
# include "XoshiroCpp.hpp"

int main()
{
    using namespace XoshiroCpp;

    const std::uint64_t seed = 12345;

    Xoshiro256PlusPlus rng(seed);

    for (int i = 0; i < 5; ++i)
    {
        std::cout << DoubleFromBits(rng()) << '\n';
    }
}
0.553048
0.204956
0.0851232
0.17553
0.265614

# include <iostream>
# include "XoshiroCpp.hpp"

int main()
{
    using namespace XoshiroCpp;

    // This example seed sequence { 111, 222, 333, 444 } is poorly
    // distributed (has a lot of '0' bits) and it is not suitable
    // for directly use in the generator's internal state.
    // SplitMix64 PRNG can be used to increase entropy.
    const Xoshiro256Plus::state_type initialStateA =
    {
        SplitMix64{ 111 }(),
        SplitMix64{ 222 }(),
        SplitMix64{ 333 }(),
        SplitMix64{ 444 }(),
    };

    Xoshiro256PlusPlus rngA(initialStateA);

    for (int i = 0; i < 3; ++i)
    {
        std::cout << rngA() << '\n';
    }

    const Xoshiro256Plus::state_type state = rngA.serialize();

    Xoshiro256PlusPlus rngB(state);

    for (int i = 0; i < 3; ++i)
    {
        std::cout << std::boolalpha << (rngA() == rngB()) << '\n';
    }

    rngB.deserialize(initialStateA);

    for (int i = 0; i < 3; ++i)
    {
        std::cout << rngB() << '\n';
    }
}
9228892280983206813
11892737616047535485
12786908792686548306
true
true
true
9228892280983206813
11892737616047535485
12786908792686548306

Roadmap

  • SplitMix64
  • xoshiro256+
  • xoshiro256++
  • xoshiro256**
  • xoroshiro128+
  • xoroshiro128++
  • xoroshiro128**
  • xoshiro128+
  • xoshiro128++
  • xoshiro128**
  • xoroshiro64*
  • xoroshiro64**

License

Xoshiro-cpp is distributed under the MIT license.

More Repositories

1

PerlinNoise

Header-only Perlin noise library for modern C++ (C++17/C++20)
C++
566
star
2

Laptops

💻 ゲーム開発・プログラミングにおすすめの低予算ノート PC 💰
152
star
3

tenkei_90

競プロ典型 90 問 練習 (C++17)
46
star
4

Siv3D-Reference

[deprecated] Siv3D Reference
C++
30
star
5

games

様々なゲームのプログラムを C++ & Siv3D で書いていくプロジェクト
23
star
6

NamedParameter

Simple named parameter interface for modern C++
C++
21
star
7

EnumBitmask

A C++ library to enable bitmask operators for an enum class type
C++
12
star
8

SivMetal

An experimental Metal project for OpenSiv3D
Objective-C++
12
star
9

Zenn.Public

11
star
10

YesNo

C++
5
star
11

IndexedIterator

Header-only indexed iterator library for modern C++
C++
5
star
12

YatCoder

競技プログラミング(プログラミングコンテスト) C++14 ライブラリ
C++
4
star
13

Guetzli-test

4
star
14

Siv

Utilities for VC++
C++
2
star
15

RandomHints

https://qiita.com/Reputeless/items/70f737e78827057b7dd1
C++
2
star
16

Reputeless

2
star
17

Siv3D-GameJam-11-RiseInSeaLevel

第 11 回 Siv3D Game Jam (テーマ「島」) 参加作品
C++
1
star
18

Siv3D-GameJam-16-2DVisibility

第 16 回 Siv3D Game Jam (テーマ「光」) 参加作品
C++
1
star
19

SivShader

C++
1
star
20

Kemurikusa-Siv3D

https://twitter.com/Reputeless/status/1119952385034440704
C++
1
star