Giter Site home page Giter Site logo

fsrandom-pcg's Introduction

The PCG Pseudo-Random Number Generator for F#

NuGet AppVeyor

About PCG

PCG is a family of simple fast space-efficient statistically good algorithms for random number generation. Unlike many general-purpose RNGs, they are also hard to predict. You can read more about PCG here.

System.Random is not so good

System.Random has some problems that might be unsuitable for some users. Specifically:

  • It uses an obscure algorithm to generate random numbers.

    According to Microsoft:

    The current implementation of the Random class is based on Donald E. Knuth's subtractive random number generator algorithm. For more information, see D. E. Knuth. "The Art of Computer Programming, volume 2: Seminumerical Algorithms". Addison-Wesley, Reading, MA, second edition, 1981.

    Try to google it. And then, try to google "Mersenne Twister" or "linear congruential generator". Did you see the diference? The obscurity of this algorithm can be important. While it is designed by a well-respected computer scientist, it's very possible that its output is not random enough, or even worse, it can be easily predicted. Imagine that you are playing a game, and your opponent can predict the roll of a dice or the order of a shuffled deck. Predictability directly contradicts the very definition of randomness. While no software can generate completely random material, it can give a decent try; and it's doing well. Furthermore, System.Random's implementation has a serious defect.

  • This is more specific to F# users, but, regardless of the underlying algorithm, the System.Random class has mutable state, an antipattern of functional programming. For example, with immutable values, generating the same random stream again might be a little hard in a stateful, imperative language like C#. Uses of System.Random have an inherent cost of adding side effects, something functional programmers are scared of. But don't worry. F# has a good library for functional random number generation.

PCG is better

Taken from here (modified):

  • It's really easy to use, and yet its very flexible and offers powerful features (not all of them are yet available on this library) (including some that allow you to perform silly party tricks).
  • It's very fast, and can occupy very little space (only 32 bytes of state, in comparison with System.Random's 232 bytes of state, and with Mersenne Twister's 2.5 KiB of internal state).
  • It has small code size.
  • It's performance in statistical tests is excellent (see the PCG paper for full details).
  • It's much less predictable and thus more secure than most generators, although I would not advise it for cryptographic usage.
  • The reference implementation is open source software, with a permissive license (the Apache license).

Documentation

The documentation of the library is in the wiki. You can also check the source code comments.

fsrandom-pcg's People

Contributors

teo-tsirpanis avatar

Watchers

Steffen Forkmann avatar James Cloos avatar  avatar

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.