Giter Site home page Giter Site logo

pcg.h's Introduction

pcg.h

Single header, 100 line implementation of 32bit PCG random number generator.

Advantages

  • Blazing fast generation of uint32 and 32bit floats in [0,1)

  • Each rng class instance only takes 128bits of memory (two uint64)

Disadvantages

Doesn't conform to full C++11 rng engine requirements. It's a very simple rng that produces output fast.

API

Constructor

  • Based on nanosecond clock and thread id

  • Should guarantee different rng values in most usecases

    • You can also manually seed by providing two uint64_t
    • The first being the seed, second the sequence id

Generating rng values

  • next() generates a uniformly distributed uint32_t

  • next_bounded(uint32_t) generates a bounded uint32_t (exclusive)

  • next_float() generates a float in [0,1)

Requires C++11 for default seeding. Specifically <thread> and <chrono>.

Example

#include "pcg.h"

int main() {
    // Create new instance of RNG
    // Default seed is different for all threads and nanosecond-time based
    pcg::pcg32 rng;

    for (int i = 0; i < 99; ++i) {
        std::cout << rng.next() << std::endl;
        std::cout << rng.next_float() << std::endl;
        std::cout << rng.next_bounded(101) << std::endl;
    }

    return 0;
 }

pcg.h's People

Contributors

vhranger avatar

Stargazers

 avatar  avatar  avatar  avatar Aarni Koskela avatar Albert Tavares de Almeida avatar  avatar Pavel Konovalov avatar Diana avatar Joakim Brännström avatar  avatar radr avatar toge avatar

Watchers

James Cloos avatar  avatar

pcg.h's Issues

Undeclared Identifier 's'

Greate implementation! Thank you for sharing.
I tried to compile with Clang and I get the following error:
pcg.h|83|error: use of undeclared identifier 's'

Not been able to solve the issue. Could please assist?
Thank you in advance!

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.