Coder Social home page Coder Social logo

morinim / xoshiro256ss Goto Github PK

View Code? Open in Web Editor NEW
9.0 3.0 1.0 177 KB

A simple `UniformRandomBitGenerator` wrapper for the xoshiro256*** and xoroshiro128+ PRNGs

License: Mozilla Public License 2.0

C++ 100.00%
cpp cpp11 prng xoroshiro xoshiro distributions random cpp20 xoroshiro128 xoshiro256

xoshiro256ss's Introduction

Dilbert Random Number Generator

A simple UniformRandomBitGenerator wrapper for the xoshiro256** and xoroshiro128+ PRNGs (see https://prng.di.unimi.it/).

Features

  • Compatible with the C++11 <random> library. The engine can be plugged into any random number distribution in order to obtain a random number.

  • Directly derived from the public-domain C implementations: 1, 2.

  • Take advantage of C++20 features.

Usage

#include <iomanip>
#include <map>

#include "xoshiro256ss.h"

void test_xoshiro256ss()
{
  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;

  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 << "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();
}

and the output is something like:

XOSHIRO256**
  0 *********
  1 *********
  2 *********
  3 *********
  4 *********
  5 *********
  6 *********
  7 *********
  8 *********
  9 *********
 10 *********
XOROSHIRO128+
  0 *********
  1 *********
  2 *********
  3 *********
  4 *********
  5 *********
  6 *********
  7 *********
  8 *********
  9 *********
 10 *********

(source code)

Remarks

xoshiro / xoroshiro generators make up a big family. Since we wanted to keep code clean and maintenable, only xoshiro256** and xoroshiro128+ are implemented.

They're all-purpose (not cryptographically secure) generators with excellent speed, a large state space and cover almost all general use cases.

By default use xoshiro256**. If you're tight on space use xoroshiro128+.

References

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.