Git Product home page Git Product logo

randomness's Introduction

Randomness

How do we code Randomness ?

Hardware (Non-Deterministic/True/Physical) RNG

  • Generates random numbers from a physical process capable of producing entropy (in other words, the device always has access to a physical entropy source)
  • Nature provides ample phenomena that generate low-level, statistically random "noise" signals, including thermal and shot noise, jitter and metastability of electronic circuits, Brownian motion, atmospheric noise, radioactive decay.
  • Services/Devices: USBs, Quantum RNGs, Free Running Oscillators (FROs)

Pseudo (Deterministic) RNG

  • Utilizes a deterministic algorithm to generate a sequence of numbers whose properties approximate the properties of sequences of random numbers.
  • The PRNG-generated sequence is not truly random, because it is completely determined by an initial value, called the PRNG's seed (which may include truly random values).
  • Important in practice for their speed in number generation and their reproducibility.

PRNG Algorithms

1. Linear Congruential Generators

The generator is defined by the recurrence relation:

Xn+1 = (aXn + c) mod m

m is "modulus"
a is "multiplier"
c is "increment"
X0 is "seed" value

Implementation modulus: m increment: c multiplier: a
C++ 11's minstd_rand 231-1 48271 0
Java's java.util.Random 248 25214903917 11
These combinations of values are most suitable for getting long periods (steps before which LCG funtion loops).

Python Code

2. xorshift128+

From Javascript's v8 engine official source code:

static inline void XorShift128(uint64_t* state0, uint64_t* state1) {
    uint64_t s1 = *state0;
    uint64_t s0 = *state1;
    *state0 = s0;
    s1 ^= s1 << 23;
    s1 ^= s1 >> 17;
    s1 ^= s0;
    s1 ^= s0 >> 26;
    *state1 = s1;
  }

Random() funtion in progamming languages

  1. Javascript
    • Math.Random() implementation depends on engine (browser).
    • v8 engine uses xorshift128+
  2. Python
    • CPython uses the Mersenne Twister as the core generator. It produces 53-bit precision floats and has a period of 2**19937-1.
  3. Java
    • Uses a modified linear congruential formula.
    public synchronized void setSeed(long seed) {
    this.seed = (seed ^ 0x5DEECE66DL) & ((1L << 48) - 1);
    haveNextNextGaussian = false;
    }
    
    protected synchronized int next(int bits) {
    seed = (seed * 0x5DEECE66DL + 0xBL) & ((1L << 48) - 1);
    return (int) (seed >>> (48 - bits));
    }
  4. C++

Testing Random numbers

RNGs can be tested against statistical test suites:

  1. Dieharder
  2. TestU01
  3. PracRand

randomness's People

Contributors

ishankjena avatar

Watchers

 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.