Giter Club home page Giter Club logo

defold-random's Introduction

Defold - PCG Random

PCG Random Number Generator Native Extension for the Defold Game Engine

This extension allow you to generate random numbers using minimal C implementation of PCG.

It uses entropy seed internally with fallback to time based seed. You can switch to Time based seed and remove the entropy by uncommenting/commenting a few lines on the source code, but I don't think it is necessary.

Installation

You can use PCG Random in your own project by adding this project as a Defold library dependency. Open your game.project file and in the dependencies field under project add:

https://github.com/selimanac/defold-random/archive/master.zip

Usage

rnd.seed(init_state, init_seq)

Seeds the random number generator.
Random number generator is always initialized by using entropy seed. You don't need to call this method unless you want to control the seed.

init_state is the starting state for the RNG, you can pass any 64-bit value.
init_seq selects the output sequence for the RNG, you can pass any 64-bit value, although only the low 63 bits are significant.

Caution: I don't recommend using of 64-bit integers. Consider using 32-bit integers instead.

rnd.seed()

Re-seed the random number generator by using entropy seed.
Random number generator is always initialized by using entropy seed. You don’t need to call this method unless you want to re-seed.

rnd.number()

Returns a 32 bit unsigned integer.

rnd.range(min, max)

Returns a 32 bit unsigned integer between min and max values. Only for positive numbers(unsigned integers).
Same as math.random(3,20)
math.random(90) == rnd.range(1, 90)

rnd.double()

Returns a floating point between 0-1.
Same as math.random()

rnd.double_range(min, max)

Returns a floating point between min - max.
Not fast as rnd.double()

rnd.toss()

Toss a coin. Returns 0 or 1 (0 = 'H', 1 = 'T')

rnd.roll()

Roll the dice. Returns between 1-6

rnd.check()

Testing entropy.

Release Notes

1.2.6

  • Fix for #6
  • Fix for #7
  • Auto-complete for native extensions

1.2.5

  • Fix for #4

1.2.4

  • rnd.double_range(min, max) added.

1.2.3

  • rnd.range returns min if min == max.

1.2.2

  • rnd.range was causing a crash when MIN is bigger than MAX. Error message added.

1.2.1

  • Added static seed
  • Small fix.

1.1

  • Fixed integers.

1.0

Initial release.

defold-random's People

Contributors

selimanac avatar thinknathan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

defold-random's Issues

Crash when range is invalid

Would it be possible to throw an error for code like this?

rnd.range(1,0)

Currently it produces a hard crash with no error trace.

Create a release

That way we can point to the release instead of a sha1 or master.

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.