Giter Site home page Giter Site logo

rossilor95 / lcg-python Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 27 KB

A simple implementation of the Linear Congruential Generator (LCG) algorithm in Python

License: GNU General Public License v3.0

Python 100.00%
algorithms python random random-number-generators linear-congruential-generator

lcg-python's Introduction

Linear Congruential Generator (LCG) in Python

For quantitative applications such as numerical simulations, it is convenient to have full control over the Pseudo-Random Number Generator we choose to use. For this reason, I implemented my own version of the Linear Congruential Generator (LCG), a simple and reliable algorithm.

Brief Theoretical Background

An LCG is a method for generating real non-negative pseudo-random numbers from the uniform distribution on $(0, 1)$.

First, an LCG produces a sequence of non-negative integers called states according to the recurring relation

$$x_i = (a \cdot x_{i-1} + c) \ mod \ m \qquad with \ i = \{0, 1, ...\}$$

Where

  • $m > 0$ is the modulus; mod m stands for "modulo m", which means you divide by m and take the remainder.
  • $x_i = \{0, 1, ..., m - 1\}$ is the i-th state of the generator
  • $x_0 = \{0, 1, ..., m - 1\}$ is a non-negative integer constant called seed. The seed is the initial state of the generator
  • $a = \{0, 1, ..., m - 1\}$ is a non-negative integer constant called multiplier
  • $c = \{0, 1, ..., m - 1\}$ is a non-negative integer constant called increment

The integer constants m, a, c and x_0 specify the generator.

For each state generated by the LCG, we can take

$$u_i = \frac{x_i}{m}$$

Where $u_i$ is a real pseudo-random number from the uniform distribution on $(0, 1)$.

About this Repo

This small project explores a simple implementation of the LCG as a Python generator. All the code is contained in the lcg.py file and is pretty straightforward.

A detailed list of good values for the LCG constants m and a can be found at:

L’ecuyer, Pierre. "Tables of linear congruential generators of different sizes and good lattice structure." Mathematics of Computation 68.225 (1999): 249-260.

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.