{"id":15722525,"url":"https://github.com/alchimystic/defold-rng","last_synced_at":"2025-10-25T16:17:43.030Z","repository":{"id":199529714,"uuid":"703043479","full_name":"alchimystic/defold-rng","owner":"alchimystic","description":"Multi-algorithm Random Number Generator Native Extension for the Defold Game Engine","archived":false,"fork":false,"pushed_at":"2023-10-16T10:52:25.000Z","size":54,"stargazers_count":9,"open_issues_count":1,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-20T23:32:37.165Z","etag":null,"topics":["defold","defold-extension","defold-game-engine","defold-library","defold-module","defold-native-extension","mersenne-twister","pcg","random-number-generators"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/alchimystic.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2023-10-10T13:41:50.000Z","updated_at":"2024-11-19T21:54:49.000Z","dependencies_parsed_at":"2024-10-24T16:50:44.943Z","dependency_job_id":"2b3516a7-eaae-45fd-993d-996876e84728","html_url":"https://github.com/alchimystic/defold-rng","commit_stats":null,"previous_names":["alchimystic/defold-rng"],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alchimystic%2Fdefold-rng","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alchimystic%2Fdefold-rng/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alchimystic%2Fdefold-rng/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/alchimystic%2Fdefold-rng/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/alchimystic","download_url":"https://codeload.github.com/alchimystic/defold-rng/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253925078,"owners_count":21985242,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["defold","defold-extension","defold-game-engine","defold-library","defold-module","defold-native-extension","mersenne-twister","pcg","random-number-generators"],"created_at":"2024-10-03T22:08:17.870Z","updated_at":"2025-10-25T16:17:38.005Z","avatar_url":"https://github.com/alchimystic.png","language":"C","funding_links":[],"categories":["Libraries"],"sub_categories":["Programming Language"],"readme":"# Defold - RNG\n\nMulti-algorithm Random Number Generator Native Extension for the Defold Game Engine, using instances (not globals)\n\nThe idea for this extension came from [defold-random](https://github.com/selimanac/defold-random), but i decided to improve it and widen the scope.\n\nThis extension has 3 aspects:\n* Provide instance based Random Number Generators, not a single global RNG. This benefits procedural generation\n* Support for multiple algorithms (the best/fastest/safest). Currently we have PCG32 and TinyMT32\n* Aimed at performance, implemented in minimal C (with a C++ wrapper for Lua binding)\n\n\n## Supported algorithms\n\n### PCG32\n\nRegarding PCG32, this extension allow you to generate random numbers using minimal [C implementation of PCG](http://www.pcg-random.org/using-pcg-c-basic.html).\n\nIt uses [entropy](https://github.com/imneme/pcg-c/blob/master/extras/entropy.c) 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. \n\n\n### TinyMT32 (Tiny Mersenne Twister)\n\nAs the original Mersene Twister has a relatively large buffer (~2.5kb) for instance based RNG i decided to go for [TinyMT](https://github.com/MersenneTwister-Lab/TinyMT), in the 32 bit variant.\n\nThe seed takes one argument only, and if none provided, we seed using a table of 4 random values, taken from entropy (just like PCG32)\n\n\n## Installation\nYou can use defold-rng in your own project by adding this project as a [Defold library dependency](http://www.defold.com/manuals/libraries/). Open your game.project file and in the dependencies field under project add:\n\n\thttps://github.com/alchimystic/defold-rng/archive/master.zip\n\t\n---\n\n\n## Usage\n\n### RNG\n\n*rng* is the global table we can use to create all the generators, providing or not an initial seed.\n\nSo as we add new algorithms, we will also add methods to this table.\n\n#### rng.pcg32()\n\nCreates a PCG generator instance with a entropy-based seed. You should use this if you don't care about explicitly providing a seed.\n\n#### rng.pcg32(`init_state`, `init_seq`)\n\nCreates a PCG generator instance given the initial state and seq for seed. You should use this to specify the seed.\n`init_state` is the starting state for the RNG, you can pass any 64-bit value.  \n`init_seq` selects the output sequence for the RNG, you can pass any 64-bit value, although only the low 63 bits are significant.\n\n#### rng.tinymt32()\n\nCreates a TinyMT32 generator instance with a entropy-based seed of 4 elements. You should use this if you don't care about explicitly providing a seed.\n\n#### rng.tinymt32(`seed`)\n\nCreates a TinyMT32 generator wth a given seed. You can pass any 64-bit value, but 0 will have the same result as a random seed.\nYou should use this to specify the seed.\n\n\n**Caution:** I don't recommend using of 64-bit integers. Consider using 32-bit integers instead. \n\n### Common Methods (to all RNG instances)\n\n#### number()\n\nReturns a 32 bit unsigned integer.\n\n#### range(`min`, `max`)\n\nReturns a 32 bit unsigned integer between min and max values. Only for positive numbers(unsigned integers).\nSame as **math.random(3,20)**  \n\n#### double_num()\n\nReturns a floating point between 0-1.  \nSame as **math.random()**\n\n####  double_range(`min`, `max`)\n\nReturns a floating point between min - max.  \nNot fast as `double()`\n\n####  toss()\n\nToss a coin. Returns 0 or 1 (0 = 'H', 1 = 'T')\n\n####  roll()\n\nRoll the dice. Returns between 1-6\n\n\n### PCG32\n\nBesides the coomon methods, a PCG32 instance has the following methods:\n\n#### seed(`init_state`, `init_seq`)\n\nSets the new seed for this instance, given state and seq.\n\n### TinyMT32\n\nBesides the coomon methods, a TinyMT32 instance has the following methods:\n\n#### seed(`seed`)\n\nSets the new seed for this instance, given the actual seed value. If seed == 0, it uses a random seed based on entropy\n\n\n## Release Notes\n\n1.1\n\nAdded support for TinyMT32 (Tiny Mersenne Twister, 32 bits)\n\n1.0\n\nInitial release.\n\n- rng: supports creation of pcg32 instance with random seed (based on entropy)\n- rng: supports creation of pcg32 instance with initial state and increment seed\n- pcg32 instance supports: seed, number, double_num, range, double_range, toss and roll\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falchimystic%2Fdefold-rng","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Falchimystic%2Fdefold-rng","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Falchimystic%2Fdefold-rng/lists"}