Code Monkey home page Code Monkey logo

lcmwc's Introduction

Build Status license MIT Licence

Lua Persistent CMWC-4096 PRNG

Lua persistent pseudo random number generator. Internally based on CMWC4096 algorithm by George Marsaglia .This algorithm gives good statistical results, it has huge period and it also very performant (original algorithm of course). Includes versions for 32 and 53 bits precision floats.

Cmwc.make( seed: number )

Create new CMWC-4096 PRNG state. Initialization procedure is based on LCG and is borrowed from libtcod sources. Returns state and initial index (1)

Cmwc.rand( state: number[4097], index: number )

Generates unsigned 32-bits integer, returns number, updated state, new index

Cmwc.random32( state: number[4097], index: number, min: number, max: number )

Generates new pseudorandom number and update state. Returns 3 values: number, updated state, next index. This function API tries to somehow mimic one from the original Lua.

When called without arguments, returns a pseudo-random float with uniform distribution in the range [0,1). When called with two integers m and n, math.random returns a pseudo-random integer with uniform distribution in the range [m, n]. (The value n-m cannot be negative and must fit in a Lua integer.) The call math.random(n) is equivalent to math.random(1,n).

Cmwc.random64( state: number[4097], index: number, min: number, max: number )

Takes 2 numbers from the state to create pseudorandom number. This is much more precise than Cmwc.random32 but 2 times slower

Performance

Simple benchmark included with comparsion with builtin randoms is provided. For LuaJIT on my notebook it's x2 performance drop, for vanilla Lua x7. For doubles its x4 and x14.

lcmwc's People

Contributors

iskolbin avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

lcmwc's Issues

cmwc.random64 bug when used with (min, max)

First of all, thanks for making this available.

I spotted a small bug in cmwc.random64 at

lcmwc/cmwc.lua

Line 113 in d536f39

return q * floor( max - min ) + min, qc, i

Where you reference a previously undefined q (I guess you meant a?)

Just to confirm the behaviour (I'm admittedly not on that great footing with prngs)
is cmwc.random64 supposed to return a float between [min, max] and cmwc.random32 should return an int?

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.